+ 3

HTML and JavaScript Issue

Ok, this is my first post and I need help. I am trying to change picture using onclick function. But cannot get the picture to change. What am I doing wrong? <!DOCTYPE html> <html> <title>HTML and JavaScript</title> <body> <h1>First Try At JavaScript</h1> <img id="Images" src="Images/logo1.jpg" width="250" height="250"> <p>Click the button to change the picture.</p> <button onclick="myFunction()">Try Me</button> <script> function myFunction() document.getElementById("Images").src = "Images/logo2.jpg"; document.getElementById("Images").src = "Images/helmet1.jpg"; document.getElementById("Images").src = "Images/helmet2.jpg"; document.getElementById("Images").src = "Images/watt1.jpg"; document.getElementById("Images").src = "Images/watt2.jpg"; </script> <p>Go Steelers!</p> </body> </html>

21st Jul 2018, 12:14 AM
Christopher Conlon
Christopher Conlon - avatar
6 Respuestas
+ 2
Thanks. I am using local files. Just cant get image to change using onclick. I will try uploading images.
21st Jul 2018, 12:32 AM
Christopher Conlon
Christopher Conlon - avatar
+ 2
Hi Christopher Conlon, Some things I see: 1. your function isn't enclosed. the curly braces are missing. 2. onclick will not cycle through the images because you haven't told it what logic to use to choose the picture file, so clicking the button would always end with the last image. For anything more, it would be really helpful if you could put this in Code Playground and link here. Hope this helps for now. ☺
21st Jul 2018, 12:24 PM
Janning⭐
Janning⭐ - avatar
0
You are using local files on your computer. Try uploading them to an image hosting sites (e.g Imgur, Dropbox ...) and copy the link to your code. Also, you are making calls to change the image like, 5 times, but only the last one showed. Not sure this is intentional or not, as it lowers performance and doesn't bring back any benefit.
21st Jul 2018, 12:30 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar
0
Thanks, everyone. I posted the code to the playground. Would like input,
22nd Jul 2018, 2:36 PM
Christopher Conlon
Christopher Conlon - avatar
22nd Jul 2018, 9:11 PM
Janning⭐
Janning⭐ - avatar
0
Yes, but I need to change image source since i actually have the files on my computer. I got this to work thanks to everyone. I use brackets as my code editor.
22nd Jul 2018, 9:14 PM
Christopher Conlon
Christopher Conlon - avatar