0

In chrome, this line of code returns something useful.

$('[data-buyername]').data()

obviously, it will return the value of the tag data-buyername='somethingArbitrary'

In IE8 though, .data() doesn't return anything.

Does anyone know how to get the data (both key and value) from a tag like this?

...

In chrome, .data() returns a dictionary {'buyername':'somethingArbitrary'}, but IE8 returns an empty dictionary {}

itcropper
  • 752
  • 1
  • 7
  • 21
  • 1
    What version of jQuery are you using? – Ram Oct 21 '13 at 21:24
  • 1
    http://stackoverflow.com/questions/6509841/jquery-data-not-retrieving-data – Kirk Backus Oct 21 '13 at 21:24
  • 1
    When you call `.data()` with no parameters, you get an **object** back, not a string. To get the "data-buyername" value from an element as a string you have to use `.data("buyername")` – Pointy Oct 21 '13 at 21:27
  • Im ok with getting back a dictionary rather than a string, but IE8 isn't even doing that. Also, all of the keys I'm using are arbitrary, so I need the call to return the key and value, and therefore have to use .data() instead of .data('key') – itcropper Oct 21 '13 at 21:30

1 Answers1

0

I didn't specifically try this in IE8 but you could see if manually looking through the element's attributes works:

var buyerData = $('[data-buyername]').get(0); // Returns the vanilla DOM element

function getData(el) {
    var o = {},
        attrs = el.attributes,
        attr = null;

    for (var i in attrs) {
        attr = attrs[i];
        if (attr.name && attr.name.indexOf('data-') != -1) o[attr.name] = attr.value;
    }

    return o;
}

var data = getData(buyerData);
console.log(data);
Todd
  • 1,674
  • 13
  • 13