Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 663
ForbesLindesay Avatar asked Mar 14 '26 03:03

ForbesLindesay


1 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

like image 144
Andreas Avatar answered Mar 16 '26 16:03

Andreas