1

I m newer to javascript, Now i am getting started to javascript.

Here is the link(run with example):

http://www.w3schools.com/js/tryit.asp?filename=tryjs_intro_lightbulb

Here, i have raised one doubt,

  if (image.src.match("bulbon")) {
//do stuff
} 

May i know, what is "bulbon" why we have use?

And i tried another one example, Here is my 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 i run my code, it works partially , that is when i click, it will becomes bright, but again i click it didn't show as dim..

May i know what is my mistake?

Thanks in advance.

2
  • image.src.match: image - Image tag, src - src attribute, match - Check whether it matches with src attribute's value.. Commented Apr 11, 2015 at 11:22
  • thank you so much for all your anwers.. I got it.. Commented Apr 11, 2015 at 11:31

6 Answers 6

2

In your if condition

if(image.src.match("bulbon"))

you are trying to match bulbon in your gif and both the gif does not have bulbon naming.

It should be like

if(image.src.match("bulbon"))
image.src="dim-bulb.jpg";
}
else {
image.src="bulbon.jpg"; }

or check the bright image correctly like this:

if(image.src.match("bright")) {
image.src="dim-bulb.jpg";
}
else {
image.src="bright.jpg";
}
Sign up to request clarification or add additional context in comments.

4 Comments

Okay @Rahul Tripathi: so may i use bri or brigh instead of bright?
@saina:- You can refer developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… to check how match works :)
that link quite complex.. but i am a newer, so can you able to tell me short? thanks
@saina:- The match method searches a string for a match against a regular expression, and returns the matches. So in your case when you try to search bulbon then it will search it in the string and if it matches then it return the match. So in your case it is "bulbon.jpg"
1

bulbon = bulb on

The code below simply checks if the image being used is the bulb with the light on and replaces the image with off version.

var image = document.getElementById('myImage');

if (image.src.match("bulbon")) {
    image.src = "pic_bulboff.gif";
} else {
    image.src = "pic_bulbon.gif";
}

To fix your code, change it to this:

function changebulb() {
    var image=document.getElementById("bulb");

    if(image.src.match("bright")) {
        image.src="dim-bulb.jpg";
    }
    else {
        image.src="bright.jpg";
    }
}

Comments

1

"bulbon" is part of the filename pic_bulbon.gif, but not pic_bulboff.gif so if(image.src.match("bulbon")) { is testing to see if the bulb is currently on.

The reason it onbly partially works is because you renamed the files - to get it working again, simply replace bulbon with bright:

<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("bright")) {
      image.src="dim-bulb.jpg";
   }
   else {
      image.src="bright.jpg";
   }
}

Comments

1

bulbon is the image name found in the src of the image tag when you see the bright light.

So when it is clicked you check the src and change the src to some other image let's say bulboff.

Your code should be like:

function changebulb() {
    // get the element with id 'bulb'
    var image=document.getElementById("bulb");
    // check the src attribute of the image tag
    if(image.src.match("dim-bulb")) {
        image.src="bright.jpg"; // change the src attribute
    } else {
        image.src="dim-bulb.jpg"; // change the src attribute
    }
}

Comments

0

Try this:

var image=document.getElementById("bulb");
if(image.src.match("dim-bulb")) {
image.src="bright.jpg";
}
else {
image.src="dim-bulb.jpg";
}

Comments

0
<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("dim-bulb.jpg")) {
 image.src="bright.jpg";
}
else {
 image.src="dim-bulb.jpg";
}
}
</script>

I think this may be answer to your question.

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.