0

I have multiple same images. When i click on one of them img need to be replaced. I have JS script:

var newsrc = "slide_down";
function changeImage() {
    if ( newsrc == "slide_down" ) {
        document.images["pic"].src = "img/slide_up.png";
        document.images["pic"].alt = "slide_up";
        newsrc  = "slide_up";
    }
    else {
        document.images["pic"].src = "img/arrow.png";
        document.images["pic"].alt = "slide_down";
        newsrc  = "slide_down";
    }
}

But when I press the second img, always the first to be replaced. Help please. Html code of image is <img src="img/arrow.png" alt="slide_up" class="head" id="pic" onclick="changeImage()">

5
  • Can you share your html and also where you applied your click handler for clear answers Commented Jun 14, 2013 at 13:37
  • all of your imgs has id="pic"? Commented Jun 14, 2013 at 13:49
  • @Cherniv Only those that need to be replaced. Commented Jun 14, 2013 at 13:51
  • you mean more than one? Commented Jun 14, 2013 at 13:53
  • @Cherniv Yes. It's img for slide up and slide down post. I have many of them. Commented Jun 14, 2013 at 14:00

1 Answer 1

2

Try

var newsrc = "slide_down";
function changeImage() {
    if ( newsrc == "slide_down" ) {
       this.src = "img/slide_up.png";
        this.alt = "slide_up";
        newsrc  = "slide_up";
    }
    else {
        this.src = "img/arrow.png";
        this.alt = "slide_down";
        newsrc  = "slide_down";
    }
}
Sign up to request clarification or add additional context in comments.

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.