Javascript onclick event declation with if-else -
Javascript onclick event declation with if-else -
i m newer javascript, getting started javascript.
here link(run example):
http://www.w3schools.com/js/tryit.asp?filename=tryjs_intro_lightbulb
here, have raised 1 doubt,
if (image.src.match("bulbon")) { //do stuff }
may know, "bulbon" why have use?
and tried 1 example, here code:
<img width="300px" height="300px" src="dim-bulb.jpg" id="bulb" onclick="changebulb()" alt=bulb"/> <script> function changebulb() { var image=document.getelementbyid("bulb"); if(image.src.match("bulbon")) { image.src="dim-bulb.jpg"; } else { image.src="bright.jpg"; } }
when run code, works partially , when click, becomes bright, 1 time again click didn't show dim..
may know mistake?
thanks in advance.
in if condition
if(image.src.match("bulbon"))
you trying match bulbon
in gif , both gif not have bulbon
naming.
it should like
if(image.src.match("bulbon")) image.src="dim-bulb.jpg"; } else { image.src="bulbon.jpg"; }
or check bright image correctly this:
if(image.src.match("bright")) { image.src="dim-bulb.jpg"; } else { image.src="bright.jpg"; }
javascript
Comments
Post a Comment