Can data attributes be created followed by a number, data-0 = "mexico"
?
Or something like this:
data-data1 = "mexico"
The idea is to retrieve them through an iteration and thus save yourself from writing attribute by attribute.
Can data attributes be created followed by a number, data-0 = "mexico"
?
Or something like this:
data-data1 = "mexico"
The idea is to retrieve them through an iteration and thus save yourself from writing attribute by attribute.
It is not necessary to use numbering to iterate over them, nor is it necessary to iterate attribute by attribute using their names. Recent versions of jQuery (if you're trying to do it using jQuery) allow you to get all attributes
data-*
by simply calling the methoddata()
with no parameters:With JavaScript you can use the element attribute
dataset
:Answering your original question: Can you create the attributes data followed by a number, data-0 = "mexico"? , the answer can be found in the HTML5 specification for data-attributes (my translation):
So for the data-attribute name to be valid it must:
data-
.:
.xml
(in any variation of upper/lower case).Although according to the XML definition the first character of a name cannot be a number, this rule does not affect the data-attributes because the first character will actually be the "d" of "data-" and not the first character after the hyphen .
Taking that into account, the attribute
data-0
is valid, as long asdata-data1
; and in fact, they both pass the W3C HTML validator successfully .