element.dataset in Internet Explorer

I need a way to list the data-* attributes of an element. I would use Object.keys(element.dataset) but IE 9.0 doesn't have dataset support. How should I do this in a way that works for IE 9.0 (and Chrome, Firefox, Safari)?

Answers


element.attributes will give you a NamedNodeList with all attributes of the element. Just check the attribute names if they start with data-

var attributes = element.attributes,
    i = attributes.length;

for (; i--; ){
    if (/^data-.*/.test(attributes[i].name)) {
        console.log(attributes[i].name);
    }
}

‚ÄčExample


Need Your Help

SQL Server Rounding Issue Looking for Explanation

sql sql-server stored-procedures rounding

I've solved this issue but I'm just wondering why this works the way it does. I have a temporary table I am selecting from and am looking to display a a name, the number of records that match this...

Displaying table from PHP in HTML through AJAX

javascript php html ajax xmlhttprequest

I am trying to display a table from my PHP file on a web page which is the index.html file using ajax. I am new to PHP and ajax so I currently do not know what is wrong with the codes. However what...