Search code examples
javascriptjqueryevent-handlingpreventdefault

Removing event.preventDefault on click doesn't work


I have this code which prevents default behavior on all elements:

$('body *').click(function(e){
e.stopPropagation();
e.preventDefault();
}); 

Now I would like to programmatically click a certain link in the page but first I have to remove the e.preventDefault(); so I used unbind:

$('a')[0].unbind('click');
$('a')[0].click();

This doesn't work for me. What am I doing wrong?


Solution

  • You can't do

    $('a')[0].unbind('click')
    

    use .eq() to get the first element and then unbind

    .eq(0).unbind('click')