2 Accessing data through data attribute
Scott Erickson edited this page 2016-02-10 09:20:16 -08:00

Problem

You want to reference the data stored inside a element's data attribute

Solution

Use jQuery's built-in data accessing method data('data_tag').

Details

Given an element with a properly formatted data tag: data-[data_tag].

- var helloWorld = "Hello, World!";
div#foo(data-bar=helloWorld)

Use jQuery's data function, we find the data assigned to data-bar.

elem = $('#foo') # Returns a jQuery object.
importantString = elem.data('bar') # Pulls the data from the 'bar' data field.
console.log importantString # Output: Hello, World!

Resources