mardi 25 octobre 2016

Want to change image src via button by javascript but code does not work

I am new to javascript. I just want to change the source of image to different one when user presses one of the 3 buttons. These buttons has different source. I don't know what is wrong with my code, apparantly it does not work...

<!doctype html>
<head>
    <title>Change image with button</title>
</head>

<body>
    <h3>Click on the buttons to change image</h3><br/><br/><br/>
    <img id="photo_mine" src="01.bmp"/><br/><br/>
    <button id="one")>1</button> 
    <button id="two">2</button> 
    <button id="three">3</button>

    <script>
        Document.getElementById("one").addEventListener("click", ch_image("01.bmp"), false);
        Document.getElementById("two").addEventListener("click", ch_image("02.png"), false);
        Document.getElementById("three").addEventListener("click", ch_image("03.jpg"), false);


        function ch_image(source_path) {
            var img_but = Document.getElementById("photo_mine");
            img_but.src = source_path;
        }

    </script>
</body>




Aucun commentaire:

Enregistrer un commentaire