0

There's an implementation of a callback to a click event for a link. I can't change or delete this code.

I want to call that link's click event callback when someone clicks a button (for example). So I decided to use .trigger('click'). It works fine.

But now, I don't want anything to happen when the user clicks the link. I was thinking of using .unbind() but doing that will make the .trigger('click') not work since the link has been unbinded.

So basically, I want to 'reuse' the callback of the link's click event without copy pasting the code. How do I do that?

  • Even if you can't change or delete this code you could provide us with it to see what's really going on ;) – SirDerpington May 03 '13 at 21:51
  • I've never tried to find an already attached event handler, but I would imagine you could do it. Get the function reference stored somewhere then unbind the event. You can 'find' existing event handlers using jQuery `data()`. http://stackoverflow.com/questions/12214654/jquery-1-8-find-event-handlers – GJK May 03 '13 at 21:53
  • There isn't a documented way of accomplishing this with jQuery, i'd suggest looking more into the plugin you are working with to see if you can get a reference to the event handler where it is defined within the plugin. – Kevin B May 03 '13 at 21:56
  • Without changing the original click event handler, you can't make it work with trigger(), and not with user clicks. Inside the click event handler you can distinguish between the two, but then you could just as well copy paste the function itself and use that instead of triggering the click. – adeneo May 03 '13 at 21:56
  • If you can bind a click event to said element prior to the event you're trying to stop, you could stop it with `event.stopImmediatePropagation()` http://api.jquery.com/event.stopImmediatePropagation/ This obviously won't work if said unchangeable code creates the element. http://jsfiddle.net/K7Rgc/5/ – Kevin B May 03 '13 at 22:05
  • Does the link need to be in the document? If not, just detach or hide it and you can still trigger its click handler but users can't click it. Having a link showing in the page that users can click but has no effect would be pretty odd. – Dave Methvin May 04 '13 at 02:03
  • Dave has a good point; added it to my answer. Hide the button and leave the handler in place. If you need the button to show then add a "dummy" button there with no handler. – HMR May 04 '13 at 02:38
  • Ok, it's not really a link. It's a fieldset title, a `` in a `` tag. I can't just hide it. –  May 06 '13 at 13:55

2 Answers2

0

This should work for you:

// Get the first click event on the link
var clickEvent = $._data( $("a.mylink")[0], "events" ).click[0];

// Unbind the click event on the link
$("a.mylink").unbind('click');

// Attach the click event to the button
$("button").click(clickEvent);

Here's a jsFiddle.

This code gets the first click event registered with the HTML element and sticks it in a variable, then unbinds the event, and attaches the event to the button. So if there are more than one events registered with the HTML element, you'd have to do some looping over $._data( $("a.mylink")[0], "events" ).click - which returns all registered click events - to find out which event to grab.

As mentioned below, the first line is using an non publicly supported internal jQuery interface, and it might change from version to version. It works with version 1.8 up to at least the current 1.9 version.

Mathijs Flietstra
  • 12,900
  • 3
  • 38
  • 67
0

Sorry missed this part:

I can't change or delete this code.

Unbind the click event from the button:

$("#mybutton").off("click");

If the code in the button click is an anonymous functon then copy that code and add it to the page so you can call it without triggering an event:

$("#mybutton").on("click",function(){
//copy this part and add it as a new function to be called.
});

If the code in button click is a function call then you can just call that function:

$("#mybutton").on("click",someFunction);

Can call that with:

someFunction();

If you have existing code you can't change that relies on $("#mybutton").trigger("click") then you might set the display of that button to none so nobody can click it, leave the event handler in place and (if button needs to be on the page) add another button on the page that has no handlers for click.

HMR
  • 37,593
  • 24
  • 91
  • 160
  • I don't want to copy the code. I want to invoke the callback that's already there. –  May 06 '13 at 13:51
  • You can copy it's html, create and insert a html after it and then hide the original. It looks the same but the copied html does not respond to clicks. You can strill trigger the click so if existing code needs to trigger clicks to work it'll still work. The problem might be that existing code sets the original html element visible again. – HMR May 06 '13 at 14:01
  • by copy it's html I mean $newEl.html($el.html()) – HMR May 06 '13 at 14:02