Skip to content

Instantly share code, notes, and snippets.

@marioblas
Last active August 29, 2015 14:00
Show Gist options
  • Save marioblas/11404424 to your computer and use it in GitHub Desktop.
Save marioblas/11404424 to your computer and use it in GitHub Desktop.
jQuery - Write data-* attribute and get it
/**
* Write data-* attribute and get it.
*
* When you make a call to .data, jQuery looks for any data-* attributes, and adds it to the data collection.
* However, THIS ONLY HAPPENS ONCE. Subsequent calls to .data will not look at the element's data-* attributes.
*
* Source: http://stackoverflow.com/questions/12271362/writing-to-a-data-attribute-and-getting-it-with-jquery-data/12271393#12271393
* Related and very important: http://stackoverflow.com/questions/7261619/jquery-data-vs-attr#7262427
*/
$('.foo').attr('data-bar', 'baz');
var dataBar = $('.foo').attr('data-bar');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment