Search code examples
javascripthtmlbuttontoggleaddeventlistener

How to toggle between two images with a button using Javascript code?


I have an HTML document that has an image, and I want to click a button to toggle between the color version and the black & white version. My javascript code is as follows is below. I realize this question has been answered before, but the answers were unclear. Questions: Is the IF condition valid? If not, what can I use? Can one compare an image.src with an address on my computer as typed below? Note that nothing is changing when I click. The color image remains.

let colorImage = document.getElementById("colorImage");
let button2 = document.getElementById("button2");

function changeToBW() {

    if (colorImage.src == "Rondout.jpg") {    // I tried three === but that 
                                              // didn't work either.
        colorImage.src = "Rondout(B&W).jpg";
}
    else {
        colorImage.src = "Rondout.jpg";
}
}
 button2.addEventListener("click", changeToBW);

A portion of my HTML code that's within the body below:

<img id = "colorImage" class = "image" src = "Rondout.jpg">
    <button id = "button2" type = "button">Change to B&W</button>

Solution

  • I copied your code to see where the problem is. I used 2 imgs that i just downloaded form google: img1.png and img2.jpeg

    It didn't worked. So I opened the DevTab of Google Chrome.

    So my code:

    let colorImage = document.getElementById("colorImage");
    let button2 = document.getElementById("button2");
    
    function changeToBW() {
    
        if (colorImage.src == "img1.png") { // colorImage.src = file:///D:/Kokal/Code/JsTests/img1.png
    
            colorImage.src = "img2.jpeg";
        }
        else {
            colorImage.src = "img1.png";
        }
    }
    
    button2.addEventListener("click", changeToBW);
    

    The problem is that colorImage.src holds the absolute path to the file. So you never end-up in the if you aways go in the else.

    Also you want to change the attribute src not the property. So you need to read the attr too. The way to do it is with the function getAttribute('src') on the colorImage. After that you will need to set that attr with setAttribute('src', [new value])

    If something is not clear chek my code below.

    HTML:

    <!DOCTYPE html>
    <html>
    <head>
        <title>test</title>
    </head>
    <body>
        <img id = "colorImage" class = "image" src = "./img1.png">
        <button id = "button2" type = "button">Change to B&W</button>
        <script src="app.js" type="text/javascript"></script>
    </body>
    </html>
    

    JS:

    let colorImage = document.getElementById("colorImage");
    let button2 = document.getElementById("button2");
    
    function changeToBW() {
    
        if (colorImage.getAttribute('src') === "./img1.png") {
            colorImage.setAttribute('src', "./img2.jpg");
        }
        else {
            colorImage.setAttribute('src', "./img1.png");
        }
    }
    
    button2.addEventListener("click", changeToBW);