identifier - Is it possible for an html5 document to have two ids? -
i wondering if html5 element can have 2 ids, this:
<button id="thing1" id="thing2" name="button1" onclick="change()">click here</button>
or this:
<button id="thing1, thing2" name="button1" onclick="change()">click here</button>
also, if 1 of ids said 1 thing, , other id said thing, 1 used?
tl;dr: no.
in versions of html, each element can have 1 id. see the spec:
the id attribute specifies element's unique identifier (id). [dom]
the value must unique amongst ids in element's home subtree , must contain @ least 1 character. the value must not contain space characters.
note: there no other restrictions on form id can take; in particular, ids can consist of digits, start digit, start underscore, consist of punctuation, etc.
note: element's unique identifier can used variety of purposes, notably way link specific parts of document using fragment identifiers, way target element when scripting, , way style specific element css.
identifiers opaque strings. particular meanings should not derived value of id attribute.
in examples gave, parser read first id. if tried this:
<button id="thing1,thing2" name="button1" onclick="change()">click here</button>
(with no space in id
), still have single id, include comma: thing1,thing2
.
note: there no reasons why want 2 id
values not candidates using class
or storing data in other way. example, in jquery, can do $('#some_id').data('foo', 'bar');
save piece of data named foo
value bar
, associate element id some_id
. classes , data storage more flexible ids, anyway.
edit: keep in mind classes aren't css. can <button id="thing1" class="thing2 thing3 thing42"...>
, use javascript (either plain or using library jquery) access button 1 or more of classes. class names can unique item , don't have used elsewhere or mentioned in css files.
here's a link jsfiddle example of how powerful classes , ids can when used together.
Comments
Post a Comment