Each() with for loop? need to iterate to add captions?

Since the captions and images are a 1:1, I would do the following: $("img"). Each(function(index){ $(this). Attr("title", $("span.

Tip"). Eq(index).text() ); }) This will cycle through each image (should make this selector more specific). With each cycle, the index (first image has index of 1, second has index of 2, etc) of the image is taken and the appropriate span (first image, first span; second image, second span, etc) is found, resulting in its text being added to the title attribute of the image You could even simplify it further: $("img").

Attr("title", function(index){ return $("span. Tip"). Eq(index).text(); }) This results in the following: ul.

Since the captions and images are a 1:1, I would do the following: $("img"). Each(function(index){ $(this). Attr("title", $("span.

Tip"). Eq(index).text() ); }); This will cycle through each image (should make this selector more specific). With each cycle, the index (first image has index of 1, second has index of 2, etc) of the image is taken and the appropriate span (first image, first span; second image, second span, etc) is found, resulting in its text being added to the title attribute of the image.

You could even simplify it further: $("img"). Attr("title", function(index){ return $("span. Tip").

Eq(index).text(); }); This results in the following: Online Demo: jsbin.com/odeciv/edit : jsbin.com/odeciv/2/edit.

Genius! Thanks! Works perfectly!

, can you explain eq()? Thank you – andrewliu Jun 27 at 17:02 @andrewliu $.eq() tests the index, or the position of the element. So the second span.

Tip has an index of 2. Basically, we're saying "If my loop is on the first image, give me the first span. Second image, second span."

See my even simpler addition which uses an implicit loop, leaving your syntax cleaner. – Jonathan Sampson? Jun 27 at 17:03 thank you thank you!

– andrewliu Jun 27 at 17:44.

You are setting the Title 3 times per , the last time (and the one that stays) is when it is being set to 'Caption 3.

No, it's not just getting the last text, it's getting each text, but as you are setting the title on all images each time you will overwrite the previous value, and all images end up with the last text. The each method provides the callback method with the index, so you can use that to get the right text for each image: $("ul"). Find('li').

Each(function(i){ $(this). Attr('title', $('#tooltips'+(i+1)).text()); }).

Try $("li"). Each(function(index){ var previewCaptions = $('#tooltips'+(index+1)).text(); $(this). Attr('title',previewCaptions); }).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions