Forum Moderators: open

Message Too Old, No Replies

If/Else statements when dealing with onclicks

if(x.onclick==true) doesn't seem to work

         

Jeremy_H

6:05 am on Nov 9, 2006 (gmt 0)

10+ Year Member



Hello,

Right now I have two functions that run if a variable is clicked.

var1.onclick=function(){...}
var2.onclick=function(){...}

What I'm trying to do is create a senario where if var1 is clicked, one thing happens. If var2 is clicked, another thing happens. And, if there is a click, put not on var1 or var2, something else happens.

I tried adding another line, which works if var1 or var2 is clicked, but two functions run if one of the variables is clicked:

var1.onclick=function(){...}
var2.onclick=function(){...}
document.onclick=function(){...}

What I'm trying to do is run an if/else statement, but I'm not having any luck.

if(var1.onclick==true){...}
elseif(var2.onclick==true){...}
else{...}

What's the right approach to if/else statements when dealing with onclicks, or other classifiers?

Thanks

daveVk

7:02 am on Nov 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The functions on var1 and var2 should return false, this will disable any further processing of the click; something like.

var1.onclick=function(){... return false;}

Jeremy_H

11:06 pm on Nov 10, 2006 (gmt 0)

10+ Year Member



Thank you for your reply,

I'm finding that even with the return false, two things events can be triggered.

var1.onclick=function(){alert("A");return false;}
var2.onclick=function(){alert("B");return false;}
document.onclick=function(){alert("Click");return false;}

If I click on var1, then it alerts "A" followed by "Click".

Do I have something setup wrong? Is it because the return false is inside the function brackets, so it stop running that one function, but continues with the rest of the script?

Anybody have any ideas?

Thanks

daveVk

2:23 am on Nov 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wonder if second onclick is result of clicking on alert OK button, or other side effect of alert. Try

var1.onclick=function(){defaultStatus="A";return false;}
var2.onclick=function(){defaultStatus="B";return false;}
document.onclick=function(){defaultStatus="Click";return false;}

and observe status display.

Jeremy_H

5:13 am on Nov 12, 2006 (gmt 0)

10+ Year Member



Thanks very much for your help.

I wasn't able to get the return false script to work, but that lead me down a path to something I was able to get to work for me.

document.onclick=function(){
if(window.event.srcElement.id=="var1"){...}
else if(window.event.srcElement.id=="var2"){...}
else{...}
}