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, apparently 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>
PS: Edited all "Document" to "document" and fixed a syntax error.
Now it's weird. The page loads always with the 3rd image (03.jpg) opened, and pressing different buttons do not change the image.
Documentdoes not exist, it should bedocument. You also have a syntax error in the HTML on button withid="one"-- there's an extra right parenthesis before the closing tag.