henhen henhen - 1 month ago 7
HTML Question

Need help understanding jquery syntax

For this segment of code:

for (var i = 0; i < numbers.length; i++) {
var imageCrystal = $('<img>');
imageCrystal.attr('data-num', numbers[i]);


is,
imageCrystal.attr('data-num', numbers[i]);
making a data attribute for the image tag that was just created and giving it a class name called
"data-num"
and then assigning whatever the value is at
number[i]
?

Answer

Simply put

imageCrystal.attr('data-num', numbers[i]); is a setter

imageCrystal.attr('data-num'); is a getter

If var numbers = [100, 200, 300] and if the img tags are appended to the DOM, it would look something in these lines.

<img data-num="100" />
<img data-num="200" />
<img data-num="300" />

More info: http://api.jquery.com/attr/