copenndthagen
copenndthagen

Reputation: 50742

jQuery event firing

If I have a code like the following;

if (x == "0")
{
     $("input:checkbox").parent().mouseover(function () {
     //Some code
     }
}

My question is will the code get executed on each mouseover OR it will first check the x == "0" condition and then fire ?

In other words, is the $("input:checkbox").parent().mouseover() code similar to what one would get with a bind() or live() function (which gets fired every time on that event) and the enclosing condition of "x" won't matter ?

Is there any way by which we can link the event with the "x" condition like limiting it's scope only if x is true?

I am not really sure if my question is really valid. But it would be really great if you could clarify.

Upvotes: 1

Views: 69

Answers (3)

JAiro
JAiro

Reputation: 5999

the event will be attached the first time that x has the value = 0. and then it will be executed on each onMousehover, you could detach the event if the x is different to 0.

if (x == "0")
{
    $("input:checkbox").parent().mouseover(function () {
    //Some code
} else {
    $("input:checkbox").parent().unbind('mouseover');
}

Upvotes: 2

Skylar Anderson
Skylar Anderson

Reputation: 5703

Once the mouseover event is attached, it will fire regardless of X. You can get the effect you are looking for by checking X inside the callback function.

 var x = 0;

 $("input:checkbox").parent().mouseover(function () {
  if(x != 0) { return; }
  // some code
 }

Upvotes: 1

Dogbert
Dogbert

Reputation: 222188

My question is will the code get executed on each mouseover OR it will first check the x == "0" condition and then fire ?

It'll get executed on each mouseover, if x == "0" at the point you have written this code.

If you want to only execute a piece of code if x == "0", try

 var x = "0"; // or any other value.
 $("input:checkbox").parent().mouseover(function () {
   if(x == "0") {
     // code goes here
   }
 }

Upvotes: 2

Related Questions