0
<!DOCTYPE html>
<html>
<head>
<script>
position=0;
var tLight = ["red light.gif","red_and_amber_light.gif","green                  light.gif","amber light.gif"]

function changeImage() {
if (position==0) {
document.getElementById('myImage').innerHTML=tLight[1]
position=1;
}    
else if (position==1) {
document.getElementById('myImage').innerHTML=tLight[2]
position=2;
}    
else if (position==2) {
document.getElementById('myImage').innerHTML=tLight[3]
position=3;
}       
else if (position==3) {
document.getElementById('myImage').innerHTML=tLight[4]
position=4;
}       

}
</script>
</head>
<body>
<img id="myImage" src="red light.gif" width="100" height="180">
<button type="button"
onclick="changeImage()">click for da lite</button>
</body>
</html>

I cant seem to find the problem in this code and i don't know whether it is me being blind or.....? someone please help. I have looked at the chrome "Developer Tools" but this cant detect any issues.

7
  • Your image URL's have spaces ? Commented Jan 22, 2016 at 14:58
  • Also, setting the innerHTML of an image ,doesn't change the src attribute ? Commented Jan 22, 2016 at 14:59
  • no i have tried this in another document but it doesn't make any difference. Thanks though Commented Jan 22, 2016 at 15:00
  • Use: setAttribute function instead of innerHTML Commented Jan 22, 2016 at 15:00
  • 2
    Odd how this question and this one look exactly alike Commented Jan 22, 2016 at 15:09

1 Answer 1

1

You are using "innerHTML" to "img" tag, you have to use "src" attribute to make it work:

document.getElementById('myImage').src = tLight[1]
Sign up to request clarification or add additional context in comments.

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.