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 5840745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:47:37+00:00 2026-05-22T11:47:37+00:00

I’m trying to paginate an image gallery using jquery. The image directory I’m using

  • 0

I’m trying to paginate an image gallery using jquery. The image directory I’m using contains about 600 images, named like this: “photo1.jpg” … “photo(n).jpg” … “photo600.jpg”.

Suppose I have a variable holding the page number var page and there are 40 images on each virtual page. I want to do something like:

for (var i = 0; i<40; i++){
      var imageNum = (((page*40)-40)+i+1);
      $("img")[i].attr("src","photo"+ imageNum + ".jpg"); // Doesn't work
   }
}

This is hackish but do you see what I am trying to do? The jquery API had some info on using each() to iterate through a jquery object but i couldn’t figure out how to send the each function a parameter holding data about which page I am on, which I will need to determine the paths to the images. I’m going for something like:

$("img").each(function (i) {
        //code to update image paths based on page parameter
      });

I’m open to different approaches as well, I am a total noob and I feel like I might be going about this in the wrong way to begin with :S.

Edit
Thanks to everyone for their answers.

If you are looking for a quick and simple answer that works using each(), check out Jeff B’s answer.

I accepted patrick dw’s answer because it goes into a bit more detail about a separate mistake I was making and covers another approach as well. Also, please see our comments on his response if you are like me and thought you needed to pass parameters to .each().

  • 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-22T11:47:38+00:00Added an answer on May 22, 2026 at 11:47 am

    This doesn’t work because [i] gives you a native DOM element that is not wrapped in a jQuery object.

    jQuery has the eq()[docs] method to give you the element wrapped so you can call the attr()[docs] method.

       // Do NOT run the selector inside the loop
    var imgs = $("img");
    
    for (var i = 0; i<40; i++){
          var imageNum = (((page*40)-40)+i+1);
          imgs.eq( i ).attr("src","photo"+ imageNum + ".jpg"); // Doesn't work
       }
    }
    

    As an alternative, you can pass a function directly to the attr()[docs] method that will loop over the elements for you.

    $("img").attr( 'src', function(i) {
        return "photo" + (((page*40)-40)+i+1) + ".jpg";
    });
    

    The return value is the value that will be assigned to the attribute you’re setting (which is src in this case).

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.