Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to use array to create some images and also attach a function to each image. I'm new to javascript, there may have many syntax errors, your correction will be highly appreciated. Here is my code below. Me and my TA worked on this for hours but still cannot make it works, anyone have a clue about this?

function changeImage() {
    var i = 0;
    element = document.getElementById(myimage[i]); // here I want it call every id or content in that array.
    if (element.src.match("bulbon")) {
        element.src = "pic_bulboff.gif";
    }
    else {
        element.src = "pic_bulbon.gif";
    }
}

var myimage = new Array();
myimage[0] = 0;
myimage[1] = 1;
myimage[2] = 2;

for (var i = 0; i < myimage.length;) {
    document.write("<img id=\"myimage[i]\" src=\"pic_bulboff.gif\" onmouseover=\"changeImage()\" width=\"20\" height=\"36\" />");
    i++; //in the img id=myimage[i] part, I want give each image I created a unique id, so my function can work on each image.
}​
share|improve this question
1  
thanks Matt for editing the format for me – Eli Dai Nov 20 '12 at 4:32
2  
you are now giving the images all the same id (a literal myImage[i]). You need to put the value of that variable there. And '0','1','2' are probably invalid ids, better start them with a letter. But you may not want to use ids at all, you could just use a class to target these images. Then you can do away with the document.write altogether and just have static HTML. – Thilo Nov 20 '12 at 4:34
Tell me how are you calling the function changeImage() and are you sure the code you posted above is not missing any parenthesis ? – Hussain Nagri Nov 20 '12 at 4:35
I agree with @Thilo – Hussain Nagri Nov 20 '12 at 4:36
Why are you setting each array element's value equal to its index? That makes the whole array redundant since looking up myindex[i] will just return the same value as i. @Thilo - id attributes can start with digits in html5‌​. – nnnnnn Nov 20 '12 at 4:42
show 5 more comments

1 Answer

up vote 0 down vote accepted

Your images don't need an id attribute at all to work with the changeImage() function that you've shown, since it seems just to be trying to swap the image source of whichever image had a mouseover event. You can instead make use of the this keyword to get a reference to the "current" image.

for (var i = 0; i < 400; i++) {
    document.write("<img src=\"pic_bulboff.gif\" onmouseover=\"changeImage(this)\" width=\"20\" height=\"36\" />");
}​

function changeImage(element) {
    if (element.src.indexOf("bulbon")!=-1) {
        element.src = "pic_bulboff.gif";
    }
    else {
        element.src = "pic_bulbon.gif";
    }
}

Note that normally the .match() method is used with a regular expression to return an array of the parts of your source string that matched the regex, but if you just want to test whether your string matches a regex without worrying about exactly what the match was then you should use .test() - or in your case since you're not using a regex at all it's probably more efficient to use .indexOf().

If you wanted a function to be able to process all of your images, you should give them a common class and then use the .getElementsByClassName() method:

for (var i = 0; i < 400; i++) {
    document.write('<img class="someClass" src="pic_bulboff.gif" onmouseover="changeImage(this)" width="20" height="36" />');
}​

function someFunction() {
    var elements = document.getElementsByClassName("someClass");
    for (var i = 0; i < elements.length; i++) {
        // do something with elements[i]
    }
}

Note that you don't need to escape all the double-quotes in a string literal with backslashes if the string was quoted with single-quotes.

EDIT - In response to the comments:

The code above definitely works, as you can see here: http://jsfiddle.net/E5TGy/

Regarding the "correct" way to make it work with arrays, the whole concept is not appropriate to this problem. It doesn't make sense to have an array where the value stored at index i is i the way you do - if looking up myimage[7] gives you 7 what's the point? The nearest thing to your code that will work is something like this:

document.write('<img id="' + myimage[i] +
               '" src="pic_bulboff.gif" onmouseover="changeImage(' + myimage[i] + 
               ')" width="20" height="36" />');

...and then your function would be:

function changeImage(elementID) {
    var element = document.getElementById(elementID);
    if (element.src.match("bulbon")) {
        element.src = "pic_bulboff.gif";
    }
    else {
        element.src = "pic_bulbon.gif";
    }
}

As shown here: http://jsfiddle.net/qKksU/

share|improve this answer
Thank you for your detailed reply @nnnnnn. I used your first version solution, but there was no image appeared at all. I just past your code to replace my code. is there any syntax issues? – Eli Dai Nov 20 '12 at 6:46
BTW, since I have spent so many time on my array version solution, I kind want to know the correct syntax for \\ element = document.getElementById(myimage[i]);\\ and \\document.write("<img id=\"myimage[i]\"......); \\ do you know the correct way to type these? @nnnnnn – Eli Dai Nov 20 '12 at 6:51
I've updated my answer to respond to your comments. – nnnnnn Nov 20 '12 at 20:25
wow.. it works perfectly. thanks a lot man! you'r awesome @nnnnnn – Eli Dai Nov 21 '12 at 9:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.