HTML5 custom attributes - Why would I use them? -
i can't seem understand why should happy html5 allowing custom attributes? why use them?
i assume you're referencing html5 [data-*]
attributes.
the advantage can associate scripting data (still semantic, not display) elements without having insert inline javascript on place, , valid html5. same thing in html4 require specifying custom namespace, , add namespaced attributes.
say you've got list of items sale, may want store numeric price without trying parse string:
<ul> <li data-price="5">item 1 $5 week!</li> <li data-price="1">sale on item 2, $1</li> ... </ul>
if allow user mark number of different items buy, can pull out numeric value display running total.
alternatively, have put numbers in span specific class, find right span on right item, , pull out value way, [data-*]
attributes reduce amount of markup/script necessary same thing.
if don't want use it, don't need to. there many ways of associating data elements, new one.
additionally, new dataset
javascript api provides consistent means of declaratively accessing values stored in [data-*]
attributes.
for jquery users, .data()
, .attr()
can used access [data-*]
attributes, , i have written detailed answer on when want use 1 on other.
Comments
Post a Comment