Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will HTML 5 data attribute support in old browsers?

I'm storing some custom data in HTML5 data attribute for Jquery processing. will the custom data attribute available in Older browsers?

like image 749
bleedCoder Avatar asked Aug 08 '13 09:08

bleedCoder


People also ask

What is data attribute in html5?

The data-* attribute is used to store custom data private to the page or application. The data-* attribute gives us the ability to embed custom data attributes on all HTML elements.

Which tag is used for data attribute in HTML?

HTML <data> Tag.

How add data attribute to select option?

HTML <select> data-* Attribute. A data-* attribute on a <select> tag attaches additional data to the dropdown control. To create a custom attribute, replace * with a lowercase string, such as data-id , data-status , or data-location .

How read data attribute in jQuery?

To retrieve a data-* attribute value as an unconverted string, use the attr() method. Since jQuery 1.6, dashes in data-* attribute names have been processed in alignment with the HTML dataset API. $( "div" ).


2 Answers

The HTML5 datalist property is not available in older browsers (it can be polyfilled easily enough though). You can always use the standard getAttribute method instead of course, and data-xxx attributes on HTML elements are accepted by all browsers (as long as you're in HTML mode and not xHTML where they're invalid)

But your question seems to be more specifically about jQuery than HTML5, and for that, the answer is Yes -- the jQuery .data() method is available in all browsers supported by jQuery.

like image 69
Spudley Avatar answered Oct 29 '22 16:10

Spudley


The attribute itself will work in all browsers. It's just an attribute after all. This would "work" in the sense that the attribute will exist in the DOM:

<div random-attribute="hello"></div> <!-- invalid, but "works" -->
<div data-random="hello"></div> <!-- valid (in browsers with HTML5 support) -->

The native dataset property of elements will not work in older browsers, but getAttribute will:

var random = document.getElementById("x").dataset.random;
// or
var random = document.getElementById("x").getAttribute("data-random");
like image 32
James Allardice Avatar answered Oct 29 '22 18:10

James Allardice