Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6072419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:09:28+00:00 2026-05-23T10:09:28+00:00

I have an issue with each loop and I’m trying to learn. I have

  • 0

I have an issue with each loop and I’m trying to learn. I have a for loop and it iterates through a set number, then what I want to do is set a caption through each images I have set up.

Jquery Code:

for(i=0; i<numImages; i++) {

    var previewCaptions = $('span#tooltips'+(i+1)).text();
    $("ul.selector").find('li').each(function(){
           $(this).attr('title',previewCaptions);
    });
}

HTML Code:

<span id="tooltips1" class="tip">Caption 1</span>
<span id="tooltips2" class="tip">Caption 2</span>
<span id="tooltips3" class="tip">Caption 3</span>

<ul>
<li><img src="src1" /></li>
<li><img src="src2" /></li>
<li><img src="src3" /></li>
</ul>

My problem is that it is only getting the last span text from the each(). How do I set up the each where it will iterate through one caption at a time instead of only getting the last one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:09:29+00:00Added an answer on May 23, 2026 at 10:09 am

    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>
      <li><img src="src1" title="Caption 1"></li>
      <li><img src="src2" title="Caption 2"></li>
      <li><img src="src3" title="Caption 3"></li>
    </ul>
    

    Online Demo: http://jsbin.com/odeciv/edit : http://jsbin.com/odeciv/2/edit

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue with drawing on canvas in a loop. What I want
I have an issue with drawing on canvas in a loop. What I want
I have made a simple test application for the issue, two winforms each containing
I am having an issue with jQuery templates. I have a simple template each
I'm having a MIPS issue here. What I'm trying to do is have the
So, I have this jQuery .each loop, and for the most part its working
I have a very simple each loop written in Ruby : params[:category].each do |i|
I have found a similar issue: NSMutableArray addObject in for loop - memory leak
I have some unit tests set up in my PowerBuilder application that run through
I have an issue with my KnockoutJS code that is creating an infinite loop.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.