Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if you use custom attribute in a HTML tag?

this question isn't related to jQuery itself but I found a plugin named Metadata found there and one of the example uses custom tag attribute: <li data="{some:'random', json: 'data'}">...</li>.

Q: Is that cross-browser? Will this fail when validating markup?

Thanks.

like image 434
Cybrix Avatar asked Oct 20 '10 01:10

Cybrix


People also ask

What is the use of custom attributes in HTML?

The data-* attributes gives us the ability to embed custom data attributes on all HTML elements. The stored (custom) data can then be used in the page's JavaScript to create a more engaging user experience (without any Ajax calls or server-side database queries).

What is the use of custom data attributes?

data-* attributes allow us to store extra information on standard, semantic HTML elements without other hacks such as non-standard attributes, or extra properties on DOM.

What is a custom attribute?

A custom attribute is an additional property that you can define to help describe business glossary assets. Labels are keywords that you can define to help describe any type of asset in the metadata repository. Users can use both custom attributes and labels to refine a search in the business glossary.

What does custom HTML mean?

An exciting feature of the HTML specification is creating custom HTML elements. These allow you to create your own HTML elements along with their own JavaScript API. This can be useful when building interfaces with reused components throughout an application.


2 Answers

The browser won't care, since very, very few browsers actually validate the HTML. It will fail if you try to treat it as XHTML though, since it isn't valid XHTML.

like image 127
Ignacio Vazquez-Abrams Avatar answered Oct 11 '22 22:10

Ignacio Vazquez-Abrams


The browser wont care. Most (if not all browsers) just ignore illegal attributes. If you try to validate it, it WILL fail however. What you need to do is figure out if you're OK with this. If you are just keep the doctype. If not change the doctype. One thing to note is that even if you keep the doctype and the illegal attribute it wont impact your site in any way that it doesnt validate.

In fact your markup might still validate if the data attribute is being added after the page loads - which means that at the point the validation occurs the data attribute wont be there.

like image 41
Darko Z Avatar answered Oct 11 '22 23:10

Darko Z