3

I have two parts of scripts.


Part 1 :
$("mySelector").click(function() {
     alert('you call me');
})

Part 2 :
$("mySelector").click(function() {
     if(myCondition) {
          //how can i prevent calling the first function from here ???
     }
})

The whole problem, is that i have no access to part1. So i need to unbind the event allready specified in part 1, if myCondition is true, but otherwise i need to call the first function.

Thanks

UPDATE:

Thank you. I didn't know about stopImmediatePropagation(). But i feel, that there must be something like that :)

But actually in my case it doesn't work :(

Please have a look at my site http://www.tours.am/en/outgoing/tours/%D5%80%D5%B6%D5%A4%D5%AF%D5%A1%D5%BD%D5%BF%D5%A1%D5%B6/Park-Hyatt-Goa/

Under the hotel description tab i have cloud carousel, when i click on not active image (not the front image), as you can see i'm consoling that i stopImmediatePropagation() there, but the event however calls :(

Simon
  • 22,637
  • 36
  • 92
  • 121

5 Answers5

5

If your handler is registered first, then you can use event.stopImmediatePropagation like this:

$("mySelector").click(function(event) { 
     if(myCondition) { 
          event.stopImmediatePropagation();
     } 
}) 

Be aware that this will also stop event bubbling, so it will also prevent click handlers on parent elements from being invoked.

Update: If this does not work, then your handler is attached after the one you want to control. This is a problem that makes the solution much more difficult. I suggest seeing if you can bind "before the other guy", otherwise you will have to unbind the existing handler and then conditionally invoke it from within your own by retaining a reference to it. See jQuery find events handlers registered with an object.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
  • @alex: I 'd gotten it wrong on the first try and deleted while I was running repairs. :) – Jon Dec 18 '11 at 14:15
  • Thank you. I didn't know about stopImmediatePropagation(). But i feel, that there must be something like that :) But actually in my case it doesn't work :( Please have a look at my site http://www.tours.am/en/outgoing/tours/%D5%80%D5%B6%D5%A4%D5%AF%D5%A1%D5%BD%D5%BF%D5%A1%D5%B6/Park-Hyatt-Goa/ Under the **hotel description** tab i have cloud carousel, when i click on not active image (not the front image), as you can see i'm consoling that i stopImmediatePropagation() there, but the event however calls :( – Simon Dec 18 '11 at 14:22
  • Nice, that can be a solution. Thank you – Simon Dec 18 '11 at 14:42
1

No access:

$("#mySelector").click(function() {
     alert('you call me');
})

Access:

var myCondition = true, //try false too
    fFirstFunction = $("#mySelector").data("events").click[0].handler;
$("#mySelector").unbind("click");
$("#mySelector").click(function() {
    if(myCondition) {
        alert(myCondition);
    } else {
        $("#mySelector").click(fFirstFunction);
    }
});

Look at this example

noob
  • 8,982
  • 4
  • 37
  • 65
1

You can call

$('mySelector').unbind('click');

to get rid of all the click handlers. If your script is loaded after the other one (which appears to be the case), then that should do it. However note that it does unbind all "click" handlers, so make sure you call that before you add your own handler.

Pointy
  • 405,095
  • 59
  • 585
  • 614
1

If you can't ensure your handler is attached first, try the following code:

var events = $('mySelector').data("events");   //all handlers bound to the element
var clickEvents = events ? events.click : null;//all click handlers bound to the element
$('mySelector').unbind('click');               //unbind all click handlers
//bind your handler
$("mySelector").click(function(e) {
   if (myCondition) {
      //do what you want
   } else {
      //call other handlers
      if (clickEvents) {
          for (var prop in clickEvents)
              clickEvents[prop].call(this, e);
      }          
   }
})

Update:

  • Above code is for jQuery 1.3.2
  • Above code is based on internal implementation of jQuery 1.3.2, so please check it carefully once you update jQuery.
ExpExc
  • 3,828
  • 1
  • 15
  • 20
-1
return false;

-or-

event.preventDefault();
naspinski
  • 34,020
  • 36
  • 111
  • 167
  • Actually this will only prevent *bubbling*, so it won't work. – Jon Dec 18 '11 at 14:13
  • `preventDefault` prevents native actions like navigating on a link; bubbling is prevented with `stopPropagation`. – pimvdb Dec 18 '11 at 14:14
  • Beware that the two are not the same thing. While `return false` stops the event handling, the `event.preventDefault()` stops the default behaviour (ex: clicking a link will issue a navigation command until prevented). – sitifensys Dec 18 '11 at 14:17