0

I am making a simple website which changes the image displayed when a button is clicked. But my code doesn't seem to be working as when I click on the button 'Click!' the alt text gets displayed instead of the image changing.The source of the images is perfectly fine, as when I use the same source outside the script the images show up.

<head>
    <title>Pic Change</title>
    <meta charset="utf-8">
    <meta name="Pic Change">
    <meta name="keywords" content="face,PES">
    <meta name="author" content="Thalle">

    <meta name="viewport" content="width=device-width,initial-scale=1.0">
</head>

<body class="body" style="background-color:#4682B4">
    <script>
    function display(whichimage){
        if(whichimage == 0){    
            document.getElementById('Click').src="C:\.....\Memes\Animals\initial.jpeg"
        }
        else{
            document.getElementById('Click').src="C:\.....\Memes\Animals\Whenlife.jpeg"
        }

    }
    </script>

    <image id="Click" src="C:\......\Memes\Animals\initial.jpg" alt="Click Button to click picture" style="width:300px;height:300px" >

    <p>
    <button type="button" onclick="display(1)">Click!</button>
    <button type="button" onclick="display(0)">Reset</button>
    </p>

</body>
</html>

5 Answers 5

1

The code is fine, you just forgot the file:// before the start. This code shows that when you give a working image src in your code, it will work just fine. Also, don't use files from your disk on Stack Overflow, it gives out private information that you probably don't want on the web.

<head>
    <title>Pic Change</title>
    <meta charset="utf-8">
    <meta name="Pic Change">
    <meta name="keywords" content="face,PES">
    <meta name="author" content="Thalle">

    <meta name="viewport" content="width=device-width,initial-scale=1.0">
</head>

<body class="body" style="background-color:#4682B4">
    <script>
    function display(whichimage){
        if(whichimage == 0){    
            document.getElementById('Click').src="http://www.iconarchive.com/download/i86425/martin-berube/flat-animal/duck.ico"
        }
        else{
            document.getElementById('Click').src="https://maxcdn.icons8.com/Share/icon/Animals//duck1600.png"
        }

    }
    </script>

    <image id="Click" src="http://www.iconarchive.com/download/i86425/martin-berube/flat-animal/duck.ico" alt="Click Button to click picture" style="width:300px;height:300px" >

    <p>
    <button type="button" onclick="display(1)">Click!</button>
    <button type="button" onclick="display(0)">Reset</button>
    </p>

</body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

0

You forgot the protocol (file://). It should be like

document.getElementById('Click').src="file://C:\Users...";

otherwise it will be just appended to the src everytime you click a button.

Comments

0

Try to put your JavaScript in the <head> section. Then it might work.

Also. There is no such thing as image in HTML. It's img.

1 Comment

No, it shouldn't. If he moves the script, that changes nothing, why would it?
0

In javascript, you need to escape backslashes in strings, so in adresses in particular. Replace all "\" by "\\".

1 Comment

try to specify the protocol: "file://C:\\Users\\..."
0

You shouldn't load images from your disk. We and others can't see it. If you use relative paths and you make sure every images and the HTML file is in the same directory, that should be fine. Even if you do, you must specify the file:// protocol. But if you use external images from a website, we could see them.

There is no <image> element in HTML. It's just <img>.

You should type \\ instead of \, because the \ character has a special meaning. However, Javascript is smart, and you can use / too, don't have to follow Windows' method.

Please don't use the onclick attribute. It's really old. Instead use event listeners.

Right now I don't know what is the problem in your code extacly, however, there is a working example:

<!DOCTYPE html>
<html>
    <title>Pic Change</title>
    <script>
        document.addEventListener("DOMContentLoaded", function() {
            document.querySelector("button").addEventListener("click", function() {
                document.querySelector("img").setAttribute("src", "file://C:/Data/300x300-colored.png");
            });
        });
    </script>
    <p><img src="file://C:/Data/300x300.png" /></p>
    <p><button type="button">Click!</button></p>
</html>

If both images and the index.html is in the C:\Data directory, it works fine.

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.