3

I'm new to Javascript, and I've done a bunch of searching but nothing I find seems to answer my specific question. I'm trying to figure out how to implement a variable into html... It might be better served with examples:

Javascript

var key = 1;

function nextImage()
{
    if(key == 52)
    {
        key = 0;
    }else{
        key++;
    }
    var image = "images/52/week_" + key + ".jpg";
    document.getElementById("mainImage").src= image;
}

HTML

<button type="button" onclick="nextImage()">Next</button>
<img id="mainImage" src="images/52/week_0.jpg" />

What I'm trying to do is move the "images/52/week_" out of the javascript and into the html, so I can reuse the function for other image sets. Basically I would like to make the script iterate through the numbers, and then send those numbers into the HTML <img src="images/52/week_" + image + ".jpg">

1
  • Are you willing to use jQuery? Commented Jun 20, 2012 at 1:56

4 Answers 4

4

HTML does not have a concept of variables. You would need the variable to be stored/manipulated in javascript. That is not to say that you cannot refactor the function to be used in a more general sense. Consider the following:

JS

var key = 1;

function nextImage(id) {
    if(key == 52)
    {
        key = 0;
    }else{
        key++;
    }

    var image = document.getElementById(id);
    var src = image.getAttribute('data-src').replace('{0}', key);
    image.src = src;
};

HTML

<button type="button" onclick="nextImage('mainImage')">Next</button>
<img id="mainImage" data-src="images/52/week_{0}.jpg" src="images/52/week_0.jpg" />
<button type="button" onclick="nextImage('otherImage')">Next</button>
<img id="otherImage" data-src="images/356/week_{0}.jpg" src="images/356/week_0.jpg" />
Sign up to request clarification or add additional context in comments.

4 Comments

The problem then is that not all my images are stored in "images/52/", some are stored in "images/365" or "images/other". I'm trying to avoid naming image locations in the "nextImage()" script. Perhaps name it somewhere on the page and pass it through?
There was no downvote. I upvoted your answer but later changed my mind, because you hadn't address the issue of flexible paths. After the edit you do, but I'm not sure the op is is looking for two image/button pairs in the same page, so I removed my upvote.
The two button/image pairs are simply to show that the function can be reused by different image sets, as that is one on the OP requirements.
You're right. I'm tired, I guess I should close SO now... Fair enough, your upvote is back.
1

Wouldnt be easier to add parameters to your function?

 var key = 1;

 function nextImage(string,id)
 {
     if(key == 52)
     {
        key = 0;
     }else{
        key++;
     }
     var image = string + key + ".jpg";
     document.getElementById(id).src= image;
 }

Update That question is answered by yourself! using the code you gave above you only need to add the two parameters to your javascript call, for example:

 <button type="button" onclick="nextImage('string','mainImage')">Next</button>
 <img id="mainImage" src="images/52/week_0.jpg" />

2 Comments

What would I do in the HTML? Where does "string" come from (where is it stored?)?
@EricWolf: onclick="nextImage('images/52/week_')">. Of course if you are doing this for several image sequences on the same site, each sequence must have its own counter.
1

You can either pass the path as a parameter, as EH_warch said, or get it from the <img> itself like this:

var key = 1;
function nextImage()
{
    if(key == 52)
    {
        key = 0;
    } else {
        key++;
    }
    var imageElement = document.getElementById("mainImage");
    var path = imageElement.getAttribute('src').split('/');
    path.pop();
    path = path.join('/');
    var image = path + '/' + key + ".jpg";
    imageElement.src= image;
}

1 Comment

Update: use imageElement.getAttribute('src') instead of imageElement.src. Also, there was a problem with my pop chaining, now fixed.
1

You can use regular expression to modify the src property.

function nextImage() {
    var img = document.getElementById("mainImage");
    var s = img.src.match(/^(.+_)(\d+)(\.jpg)$/);
    var key = s[2];

    if (key == 52) {
      key = 0;
    } else {
      key++;
    }

    img.src = s[1] + key + s[3];
}

Note: As FelixKling said in the comment, the regex pattern must match with the image filenames.

1 Comment

This is a good idea, but only works if the files all have the same name pattern. OP has to confirm this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.