Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How browsers handle invalid/unspecified attributes on HTML elements?

For examlpe, if I have this:

<div id="blah" myattribute="something">whatever</div>

Can I be safe that no browsers will ignore (and thus render inaccessible from legacy JavaScript) the myattribute? I am aware that this is ugly and not standard, but is quite useful. Or if they do, would jQuery still be able to get them?

like image 918
Kroltan Avatar asked Feb 21 '13 14:02

Kroltan


People also ask

How do browsers handle unrecognized elements?

Unrecognized HTML elements are treated by the browser as anonymous inline elements (effectively inline elements with no semantic value, similar to <span> elements). You can still refer to them by their names, and style them with CSS, for example — you just need to make sure they are behaving as you want them to.

Which is the invalid tags in HTML?

In XHTML and in HTML5 (even in HTML serialization), both <abc> and <123> are invalid. In HTML 4.01, <123> is valid, though not recommended, and it simply means those five data characters.

What does invalid HTML mean?

This means that the URL in question has defined the language/region attribute using HTML lang, but either the language code or the geography code is invalid (or both are invalid).


1 Answers

You should use data attributes, they're web standard.

Like this:

<div id="blah" data-myattribute="something">whatever</div>

Then in jQuery you can do:

var value = $("#blah").data("myattribute");
like image 76
mattytommo Avatar answered Nov 14 '22 22:11

mattytommo