tags:

views:

121

answers:

4

Morning All,

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

Thanks in advance!

Gary

Hi all,

Wow!! Thanks for all your reply's. I will try to answer all your responses. I don't know how to reply to each of your answers so I've edited this so you can all see. (Sorry i'm new to stack Overflow)

I've tried placing the alert('test') into the function and can confirm it works, in fact that's how I knew the load function was in fact working because it pauses the page until I close the alert box. In the meantime the list updates in the background.

Here is all the code I have added to the screen, its a sharepoint page so if i upload the html it will be about 200 pages long:

<script>
$(document).ready(function(){
  $('#ListSnapshot').load('http://sharepoint/BS/PMO/TR/IT%20Review/Forms/AllItems.aspx #WebPartWPQ2 .ms-listviewtable');
});

$('#ListSnapshot a').live('click', function(e){
 e.preventDefault();
 var url = $(this).attr('href') +' #WebPartWPQ2 .ms-listviewtable';
 $('#ListSnapshot').load(url);
 alert(url);
});
</script>
+1  A: 

Try returning false. live will not always work the same way as bind works. check jquery docs.

Teja Kantamneni
Thanks, im afraid ive tried that but still no luck. Thanks AgainGary
Gary
+4  A: 

The code you've provided should definitely be working (working example). There's got to be another issue with your code.

Try placing an alert inside your event handler to make sure that it fires at all. It's possible that your #ListSnapshot a isn't finding anything.

If something else is wrong inside your handler, that causes an exception, that could prevent the javascript from executing all the way to the preventDefault call. I don't see what that could be in the code you've provided, tho.

David Hedlund
I think this is probably the answer. `try..catch` should help.
Josh
Ive edited the question because i wasnt sure how to add a reply, and could not fit it all in this box.ThanksAgain
Gary
@Gary: You did the best thing -- Editing your question with detail and then commenting to let others know to review your edited question is the appropriate way to handle this. That being said, given that the alert does show, I'm not sure what the issue is. It's not an exception as I had suspected :-(
Josh
A: 

I think @David Hedlund's answer is correct, there must be an exception happening. When I write event handlers I use a try...catch block to make sure that the default action doesn't happen. Try this:

$('#ListSnapshot a').live('click', function(e){
    try {
        var url = $(this).attr('href') +' #WebPartWPQ2 .ms-listviewtable';
        $('#ListSnapshot').load(url);
    } catch(ex) {
        alert('An error occurred and I need to write some code to handle this!');
    }
    e.preventDefault();
});

That way, since e.preventDefault(); is outside the try block, even if an error occurs, e.preventDefault(); will still be called.

Josh