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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:15:03+00:00 2026-06-09T00:15:03+00:00

I am trying to write a script in jQuery that first, takes src of

  • 0

I am trying to write a script in jQuery that first, takes src of an image tag (with ID “#image”).

Then strips away the first part of the image path (“images/big/”) and the image extension (“.jpg”). This leaves just the image name, which is just a number (ranging from 1 to 7).

Then, increments that number by 1.

Then puts image path and the image extension (the stuff removed at the beginning) back in the proper place. Then assign this new path to the img tag (with ID “#image”)

Essentially the script is meant to increment the number that is the image’s name.

This is the code I managed to write:

var imagePath = ( $("#image").prop("src") );
    var imagePath = imagePath.replace('images/big/', '');
    var imagePath = imagePath.replace('.jpg', '');
    var imagePath = imagePath++;
    var imagePath = "images/big/"+imagePath+".jpg";
    $("#image").attr("src", imagePath);

Sadly, however, it does not work, its seems to just return a value of “NaN”. Any help you can give would be greatly appreciated.

  • 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-06-09T00:15:04+00:00Added an answer on June 9, 2026 at 12:15 am

    You have too much code to replace a single number.

    $("#image").attr("src", $("#image").attr("src").replace(/\d/, function(s) {
        return +s+1;
    }));
    

    Fiddle

    Or a more sturdy version (makes sure the number is followed by .jpg and may accept more than single-digit numbers):

    $("#image").attr("src", $("#image").attr("src").replace(/(\d+)\.jpg/, function(s, m) {
        return (parseInt(m, 10)+1) + '.jpg';
    }));
    

    Fiddle

    parseInt() parses a string into an integer with the radix/base specified — 10 corresponds to decimals. It’s done so the JS engine doesn’t assume that a number should be an octal if it starts with 0 for example. Well, always when you use decimals, use parseInt(myNumber, 10) to be sure that it’s parsed as decimal integer.

    The .attr you should be familiar with, it retrieves the given HTML attribute when passed 1 argument and sets the given attribute when passed 2 arguments. So basically, I’m setting the src attribute with the same src after having the replace method applied to it.

    The string.replace method takes 2 arguments, in this case I’m using a regex (Regular Expression) to match digit characters (\d is a character class representing [0-9]) followed by the literal .jpg string and replacing them with the matched number incremented by 1.

    The function object in the 2nd parameter of the replace method takes as arguments the whole match (s) followed by N number of capturing groups (m = \d+ = one or more digit characters). As I only have one capturing group, I give 2 parameters to the function so I can use the captured group and increment it inside the function object, returning the incremented number + .jpg to replace the number.jpg in the original source.

    And for the reason your code was not working, my only guess is that you weren’t stripping out all non-number characters from the string, hence when the ++ increment operator tries to force a typecast it just returns a NaN, as the given string fails to be parsed as integer. With my replace method, it takes only the number from the string and increments it, independently of the rest of the string.


    Edit:

    I found the main problem of your code, $("#image").prop("src") retrieves the FULL URL of the image, that is why, as my paragraph above implies, you weren’t removing all non-number characters and it failed to evaluate as integer. Replacing that .prop with .attr may allow your original code to work as well.

    Though you shouldn’t assign a variable to itself after using the post-increment operator ++ and declaring the same variable name in the same scope more than once is considered bad practice (most JS interpreters ignore re-declarations but it is sloppy coding).

    So another corrected version of the code:

    var imagePath = $("#image").attr("src"); //use .attr as .prop returns full URL
    imagePath = imagePath.replace('images/big/', '').replace('.jpg', '');
    imagePath++;
    imagePath = "images/big/"+imagePath+".jpg";
    $("#image").attr("src", imagePath);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a script in jQuery that would iterate through each text
I'm trying to write a Jquery script that fades in Images as they are
I'm trying to write up a simple jquery script that parses some JSON and
I am trying to write a jQuery script that will add a class to
I am trying to write my own form validation script with jQuery. However, I
I am trying to write a simple piece of Jquery in which the script
I'm trying to write a script with wsadmin that will retrieve the total amount
I'm trying to write a script that will create a user in MediaWiki, so
I'm trying to write a script that allows an admin of a photo uploading
I'm trying to write a script that will download files from an FTP server.

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.