2

On my wordpress website i have a calendar page managed by a plugin installed which has some basic links in it <a class="mylink" href="http://myurl.com">Some text</a>.

Somewhere in that plugin there is some Javascript code that once the user clicks on one of those links triggers an AJAX call. Well, I don't want this to happen. And, of course, i don't want to edit the plugin core files.

What i want to do is to simply create a script that removes any action bound to those links so that once the user clicks on one of those links nothing happens. So, i tried with jQuery methods preventDefault(), stopPropagation() and stopImmediatePropagation(), but none of them worked.

jQuery(document).on('click','.mylink', function(event){
    event.preventDefault();
    event.stopImmediatePropagation();
    event.stopPropagation();
    //do the stuff i want here
});

What am i missing?

Alberto Fontana
  • 870
  • 1
  • 14
  • 33

2 Answers2

2

Much like the on() function can be used to wire up events, you can use the off() function to remove the event handlers :

$(document).off('click','.mylink');

You'll just need to ensure that this is performed after your initial event handler was created.

Rion Williams
  • 69,631
  • 35
  • 180
  • 307
1

In your code you're just adding another handler for the click event, not replacing the original handler(s). You need to unbind click events on those links. The code to do this:

jQuery('a.mylink').unbind('click')
Alex Polkhovsky
  • 3,208
  • 5
  • 26
  • 36