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

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -