0

In the following code e.preventdefault(); isn't working, the browser is visiting the anchors href anyway. I can see the first alert, but after that the browser follows the href link of the anchor. I've read this (similiar) question on Stackoverflow, but it isn't really helping me: e.preventdefault(); not working

Any ideas on what i'm doing wrong or how I can fix it? Thanks in advance.

$('body').delegate('a.newslink', 'click', function(e) {
    alert('start');
    e.preventDefault();
    $.get( $(this).attr('href'), function(data) {
        alert(data);
        $('.reader').replaceWith(data); 
        $('#article-menu').removeClass('open');
        $('#article-menu').addClass('closed');
        $('#newslist').animate({height:'0px'}, 200);
    });
}); 
Community
  • 1
  • 1
Chris Koster
  • 403
  • 4
  • 12
  • 1
    Have you tried removing the alert? Alerts are not suitable for debugging. – Khez Apr 15 '11 at 13:12
  • 1
    have you tried with e.stopPropagation() ? – Karthik Apr 15 '11 at 13:12
  • Are you sure it's being bound as you think? When I try to do a quick jsfiddle preventDefault works fine. – justkt Apr 15 '11 at 13:16
  • 1
    Working jsFiddle: http://jsfiddle.net/AudCY/1/ Alerts have been removed, but it works with them as well. – Chris Shouts Apr 15 '11 at 13:27
  • @khez, yes tried removing the alert, i know it's not suitable but using console.log doesnt work in this particular case because the browser is following the link before i could the console. – Chris Koster Apr 15 '11 at 13:39
  • @justktk @Chris Shouts, thanks for help you guys might have put me on the right track. I've seen your fiddle in which it works. I'm now trying to find out if there some other JS actions on the link... Thanks, will keep you updated. – Chris Koster Apr 15 '11 at 13:42
  • @Chris Koster does your html vaidate ? we haven't seen that – mcgrailm Apr 15 '11 at 13:45
  • @Chris make sure you post your solution when you find it (as an answer not as an edit to the question). – lambacck Apr 15 '11 at 13:49
  • ALRIGHT GUYS HOLD YOUR HORSES! There some more javascript action on the same class later on in the script. I am sorry for wasting your time! I've been away on holiday and just got back, we work on this with two developers so things like this can happen I suppose. My bad for not checking it extensively. On the other hand your, answers put me on the right track (@justktk @Chris Shouts). Again sorry and thanks. – Chris Koster Apr 15 '11 at 13:54
  • @lamback i'm not allowd to anser my own question as a new user for 24 hours, maybe someone can copy paste it? thanks! – Chris Koster Apr 15 '11 at 13:55

1 Answers1

2

Try making the preventDefault call the first thing you do. The alert might be getting in the way.

Also because this is a delegated event, you might need to call e.stopPropagation as well.

Finally, I am not 100% sure in this case, but the event may not be cancelable (check the value of e.cancelable). See https://developer.mozilla.org/en/DOM/event.preventDefault

lambacck
  • 9,768
  • 3
  • 34
  • 46
  • Weird enough return false; isn't executing either. And gareth is right, because of it also using stopPropagation i'd have to use return false on the end of the function. – Chris Koster Apr 15 '11 at 13:46
  • ALRIGHT GUYS HOLD YOUR HORSES! There some more javascript action on the same class later on in the script. I am sorry for wasting your time! I've been away on holiday and just got back, we work on this with two developers so things like this can happen I suppose. My bad for not checking it extensively. On the other hand your, answers put me on the right track (@justktk @Chris Shouts). Again sorry and thanks – Chris Koster Apr 15 '11 at 13:55