Skip to content

Accessing data through data attribute

Scott Erickson edited this page Feb 10, 2016 · 2 revisions

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

Clone this wiki locally