0
   

I can't figure out this code at all

 
 
Reply Mon 7 Sep, 2015 06:20 am
Here is the following code:

<img id="myImage" onclick="changeImage()" src="pic_bulboff.gif" width="100" height="180">

<script>
function changeImage() {
var image = document.getElementById('myImage');
if (image.src.match("bulbon")) {
image.src = "pic_bulboff.gif";
} else {
image.src = "pic_bulbon.gif";
}
}
</script>

I want to know why is the pic_bulboff.gif image shown if the image source contains the string "bulbon". Shouldn't it be the other way around? Shouldn't the pic_bulbon.gif image be shown when there is a match in the expression with the bulbon string and pic_bulboff.gif shown otherwise?

(This code is available here in the following tutorial: http://www.w3schools.com/js/tryit.asp?filename=tryjs_intro_lightbulb)
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 0 • Views: 845 • Replies: 0
No top replies

 
 

Related Topics

 
  1. Forums
  2. » I can't figure out this code at all
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 05/07/2024 at 08:31:56