e.preventdefault(); not working

Gary picture Gary · Jul 29, 2010 · Viewed 84.6k times · Source

I'm having real trouble getting e.preventDefault(); to work.

Here is my code

$('#ListSnapshot a').live('click', function(e){
    var url = $(this).attr('href') +' #WebPartWPQ2 .ms-listviewtable';
    $('#ListSnapshot').load(url);
    e.preventDefault();
});

Could someone explain what I'm doing wrong, I can see the load function work but then the page redirects to the clicked link which I need to prevent.

I have also tried moving e.preventDefault(); to the top of the function, to no avail.

Answer

jcsierra.27 picture jcsierra.27 · Apr 2, 2013

I had a similar problem, in which e.preventDefault() would work on some cases, but not on others. It showed no errors, and using try-catch was not displaying the catch alert. Adding e.stopImmediatePropagation() did the trick, in case it helps anyone (big thanks to wcpro)