henhen henhen - 1 year ago 59
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]);

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
and then assigning whatever the value is at

Answer Source

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/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download