Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm beginner in javascript
code turn on/off light bulb image.i need to know the rule of "bulbon".
i think its the problem.
HTML
<!DOCTYPE html>
<html>
<body>
<script>
function changeImage()
{
  element=document.getElementById('myimage') 
     if (element.src.match("bulbon"))
        {
          element.src="C:\Users\MOHAMED\Desktop\pic_bulboff.gif";
        }
     else
        {
          element.src="C:\Users\MOHAMED\Desktop\pic_bulbon.gif";
        }
}
</script>

<img id="myimage"  önclick="changeImage()"
src="C:\Users\MOHAMED\Desktop\pic_bulboff.gif" width="100" height="180">

<p>Click the light bulb to turn on/off the light</p>

</body>
</html>
Posted
Updated 2-Sep-13 23:46pm
v3

XML
Hi,

Please use the virtual path instead of the physical path. Add the images to project folder and refer them. Also Use IndexOf instead of match.

code
<pre lang="Javascript"> <script type="text/javascript">
        function changeImage() {
            element = document.getElementById('myimage')
            if (element.src.indexOf("pic_bulboff") > 0) {
                element.src = "pic_bulbon.gif";
            }
            else {
                element.src = "pic_bulboff.gif";
            }
        }
</script>
 </pre>
Hope it helps...
 
Share this answer
 
Comments
mhassan083 3-Sep-13 5:58am    
But ,code is run from notepad file not project folder ,that convert to webpage to browse
Your question i need to know the rule of "bulbon".

"
When you click on the image, the javascript is invoked and the if condition is checking which image you have clicked on.

In other words if (element.src.match("bulbon")) condition checks whether the image you clicked got the name string "bulbob" on its name

"

This works for me

HTML
<html>
    <head>
        <title></title>
        <script>
            function changeImage() {
             
                element = document.getElementById('myimage')
                if (element.src.match("crm")) {
                   
                    element.src = "img/cal.png";
                }
                else {
                   
                    element.src = "img/crm.jpg";
                }
            }
</script>
    </head>
<body>
    
 
<img id="myimage"  önclick="changeImage();">
src="img/crm.jpg" height="100" width="100"/>
    
<p>Click the light bulb to turn on/off the light</p>
 
</img></body>
</html>
 
Share this answer
 
v2
Comments
mhassan083 3-Sep-13 6:10am    
please,what crm mean in code .?
Bala Selvanayagam 3-Sep-13 6:19am    
Hi,

I have two images

img/crm.jpg & img/cal.png (Both images are in a folder called img)

On the click, I have to change the image. if the image was "crm.jpg" when you clicked then it needs to be changed to "img/cal.png" and vice-versa.

In your if condition "element.src" will be the image you have clicked on and the condition is checking whether the image name you have clicked contains "crm". If true, this means you have clicked crm.jpg and then within the if clause loading the other image

Hope it make sense ?
There is nothing special in bulbon.All the magic,done over here is carried out by match() function of javascript.Refer detailed explanation of match() in below link

http://www.w3schools.com/jsref/jsref_match.asp[^]

Main concept over here is to find a match of particular word,given as an argument in match() method.In above example document.getElementById('myimage') gives you access of an HTML object pertaining to id 'myimage'.you have stored the object in a variable named 'element'.Now your next line of code says element.src.match("bulbon").It simply refers to the src attribute of img tag provided in your HTML markup.in your case element.src initially means "C:\Users\MOHAMED\Desktop\pic_bulboff.gif".To that string you are appending match('bulbon') method.This means to check whether your string contains a string 'bulbon' or not.If it contains, it returns true else it returns false.

i think there is some problem in your path of image file specified by you.Other things are logical and appropriate to the best of my knowledge.Please check it out.Hope this will help you.
 
Share this answer
 
Try this..May be it wil solve your problem

function changeImage()
{

element=document.getElementById('myimage') ;
if (element.src.indexOf("bulbon")>0)
{
element.src="../Desktop/pic_bulboff.png";
}
else
{
element.src="../Desktop/pic_bulbon.png";
}
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900