Last active
August 29, 2015 14:00
-
-
Save marioblas/11404424 to your computer and use it in GitHub Desktop.
jQuery - Write data-* attribute and get it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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