Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
172
rated 0 times [  179] [ 7]  / answers: 1 / hits: 47115  / 12 Years ago, tue, june 12, 2012, 12:00:00

I have the following code:



alert($embellishment.data(embellishmentId));
alert($embellishment.attr(data-embellishmentId));


The first alert returns undefined, whereas the second alert returns an integer, 3.



-- SEE DEMO --



I'm using jQuery version 1.7.2 (data was added with version 1.4 I believe)



Why is this? Should I be using data() at all if its not returning the right values?


More From » jquery

 Answers
7

OK. I found the problem by interpreting jQuery docs.



When you write:



$embellishment.data(embellishmentId);


it is handled by jQuery as compound attribute:



<div data-embellishment-id=3></div>


So, to solve the problem you can use lower case in the data key otherwise it just addresses the different attribute.



<!-- HTML -->
<div data-embellishmentid=3></div>

// JavaScript
$embellishment.data(embellishmentid);

[#84972] Monday, June 11, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
susand

Total Points: 690
Total Questions: 101
Total Answers: 104

Location: Lesotho
Member since Wed, Jun 2, 2021
3 Years ago
;