Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have called a simple function on html-image click to change the pic. But when i re-click on the same image , it does not changes back to the same image. please help in solving :-


JavaScript
function ChangePic() {
            if (document.getElementById("bulb").src = "IMAGES/pic_bulboff.gif")
             document.getElementById("bulb").src = "IMAGES/pic_bulbon.gif";
            else 
           document.getElementById("bulb").src = "IMAGES/pic_bulboff.gif";
            }


HTML
<img id="bulb" alt="no pic is available" />
Posted
Comments
syed shanu 8-Jan-15 22:10pm    
Check with Alert message like this
function ChangePic() {
if (document.getElementById("bulb").src == "IMAGES/pic_bulboff.gif")
{
alert("yes");
document.getElementById("bulb").src = "IMAGES/pic_bulbon.gif";
}
else
{
alert("No");
document.getElementById("bulb").src = "IMAGES/pic_bulboff.gif";
}
}

It is not related to calling a function once or twice. In all of your lines, you do assignments, you never check up condition which would return false.
Javascript conditional operator is '==', not '='. To fix your code, consider changing the line of your "if" operator: replace '=' by '=='.

—SA
 
Share this answer
 
v2
Comments
shikhar gilhotra 8-Jan-15 20:03pm    
no, its still not workable .:-(
Sergey Alexandrovich Kryukov 8-Jan-15 21:06pm    
Just do it carefully.
—SA
C#
function ChangePic()
       {
           if (document.getElementById("bulb").src== "http://localhost:1620/IMAGES/pic_bulboff.gif")
           document.getElementById("bulb").src = "IMAGES/pic_bulbon.gif";
             else
           document.getElementById("bulb").src = "IMAGES/pic_bulboff.gif";
         }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Jan-15 21:06pm    
Are you sure it's your resolution of the problem? :-)
—SA

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



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