0

How can I change the source of multiple image-tags using pure JavaScript? I'm making a memory game and if the player unveils two images which do not match I need to reset the source of these two. This is my code for doing this so far:

    if (flipLock >= 3) {

        aTag.onclick = null;
        timeoutID = setTimeout(function () {
            var reset = document.getElementsByTagName("img");
            reset.src = "../pics/0.png";
            flipLock = 0;
        }
        , 1000);
    };

3 Answers 3

1

You have to iterate

var reset = document.getElementsByTagName("img");

for (var i=reset.length; i--;) {
     reset[i].src = "../pics/0.png";
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Tibos - it iterates the opposite way, yes, but why would that matter.
@Tibos - Nope, it starts with the last element in the list, and ends with the first -> jsfiddle.net/b4QHj
Apparently i can't read properly. Didn't see that the decrementation was at the condition check. +1 for the best (and first) answer.
0

use loop

var reset = document.getElementsByTagName("img");

for (var i=0; i<reset.length; i++) {
     reset[i].src = "../pics/0.png";
}

Comments

0
var reset = document.getElementsByTagName("img");

for (var i=0; i<reset.length; i++) {
     reset[i].src = "../pics/0.png";
}

Comments

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.