Itay Moav -Malimovka
Itay Moav -Malimovka

Reputation: 53606

get the data-* attributes of a tag in jQuery

Instead of using the regular attr() is there a shorthand in jQuery to access all the data-* attributs of an element. One which enable me to just specify the name without the data- prefix, like dataset document.getElementById('id').dataset.somename;

Upvotes: 0

Views: 1819

Answers (1)

MortenMoulder
MortenMoulder

Reputation: 6656

Answer: Yes. There is a shorthand to get all the data-* attributes from an element.

To grab a single element's data value (which looked like what you wanted, but I guess it's not..?):

$("div").data("name");

That would grab the value from data-name. Example:

<div data-name="Jacob"></div>

$("div").data("name"); //"Jacob"

http://jsfiddle.net/nhzj3qtk/


To grab all the data-* attributes, you can do this:

$("div").data();

Here's an example for what you can do: http://jsfiddle.net/nhzj3qtk/1/

Upvotes: 1

Related Questions