Created
September 15, 2014 23:48
-
-
Save sabid/eb043ca627ee9dcb8ab0 to your computer and use it in GitHub Desktop.
jquery set data attr
This file contains hidden or 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
console.log($("#you").data("you")); // Hello mean | |
$("#you").data("you", "yes change you atribute"); // yes change you atribute | |
console.log($("#you").data("you")); // yes change you atribute | |
With HTML atributes | |
There has already been an answer chosen as a correct one, however its seems like none of the answers clearly and concisely explain what is happening. | |
So let me give this a shot: | |
$(element).data(key, value) does not change the html5 'data-*' attributes | |
of the element, jQuery internally stores the key-value (in jQuery.cache). | |
As a result when you call $(element).data(key, value)you get what is stored | |
internally by jQuery. | |
To answer your question here: | |
Since you are looking to change the data-you attribute of your | |
html tag you will instead need to use the attr() method | |
console.log($("#you").attr("data-you")); // Hello mean | |
$("#you").attr("data-you", "yes change you atribute"); | |
console.log($("#you").attr("data-you")); // The data-you attribute has been changed. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment