Can I add a custom attribute to an HTML tag like the following?
use data-any , I use them a lot
<aside data-area="asidetop" data-type="responsive" class="top">
Yes, you can, you did it in the question itself: <html myAttri="myVal"/>
.
well! you can actually create a bunch of custom HTML attributes by disguising the data attributes in what you actually want.
eg.
[attribute='value']{
color:red;
}
<span attribute="value" >hello world</span>
It apparently works but that would invalidate your document, there is no need of using JScript for you to have custom attributes or even elements unless you have to, you just need to treat your new formulated(custom) attributes just the same way you treat your "data" attribute
Remember "invalid" does not mean anything. The document will load fine at all the time. and some browsers would actually validate your document only by the presence of DOCTYPE....., you actually know what I mean.
I can think of a handy use for the custom tag "init"
. Include a JavaScript expression that gets evaluated at document.onLoad()
time and provides a value for the tag, e.g.
<p><p>The UTC date is <span init="new Date().toUTCString()">?</span>.<p></p>
Some boilerplate JavaScript code would scan all the tags in the DOM at document.onload()
time looking for the init attributes, evaluating the expressions that they contain, and assigning them to the containing tag's innerHTML. This would give HTML some of the power of JSP, PHP etc. Currently we have to split the HTML markup and the JavaScript code that illuminates it. Bugs love split code.
You can add custom attributes to your elements at will. But that will make your document invalid.
In HTML 5 you will have the opportunity to use custom data attributes prefixed with data-.
In HTML5: yes: use the data- attribute.
<ul>
<li data-animal-type="bird">Owl</li>
<li data-animal-type="fish">Salmon</li>
<li data-animal-type="spider">Tarantula</li>
</ul>