Thursday, 12 September 2013

JavaScript Event is not working with getElementById

JavaScript Event is not working with getElementById

I have a problem with JS Events. I placed an image on my website and with
the help of JS when one would click the image, the src attribute would be
set to find another image. But nothing happens!
CODE
<img id="img" src="Login_img1.jpg" width="425" height="219" alt="Hey"
onclick="img()">
Now the JS:
function img(){document.getElementById("img").src = "Login_img2.jpg"}
Update
Now I would like to know how do I make the image change to another one,
then change back to normal, cuz the following code doesn't works:
function change(){
img = document.getElementById("img")
img.src = "Login_img2.jpg"
img.onclick = "change2()"}
function change2(){
img = document.getElementById("img")
img.src = "login_img3.jpg"
img.onclick = "changeN()"}
function changeN(){
img = document.getElementById("img")
img.src = "login_img1.jpg"
img.onclick = "change()"}

No comments:

Post a Comment