0

All, I've got the following jQuery code:

jQuery(".select_it").click(function(e){
    e.preventDefault();
    song_id = jQuery(this).attr('href');
    music_choice = jQuery("#song_type").val();
    jQuery.post(site_url + "save_song_choice.php", { song_id: song_id, music_choice: music_choice },
        function(response) {
            alert(response);
            var url = site_url + "okyne-form";
            window.location.replace(url);
        });
        return false;
    }
);

When I click on the link, it doesn't perform the jQuery but instead takes the behavior that the link was clicked. How can I prevent this from happening?

Thanks!

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
user1048676
  • 9,756
  • 26
  • 83
  • 120
  • Because you have `window.location.replace(url)`? :o – Andreas Wong Jul 03 '12 at 02:24
  • 1
    possible duplicate of [event.preventDefault() function not working in IE. Any help?](http://stackoverflow.com/questions/1000597/event-preventdefault-function-not-working-in-ie-any-help) – McGarnagle Jul 03 '12 at 02:24
  • 1
    @dbaseman: That's incorrect. jQuery abstracts `preventDefault`. – Ry- Jul 03 '12 at 02:27
  • @minitech I don't think *e* in this case is a JQuery object... is it? – McGarnagle Jul 03 '12 at 02:28
  • @dbaseman: It is an object, like everything* else in JavaScript, with some methods modified, the most famous of which is probably... `preventDefault`. – Ry- Jul 03 '12 at 02:29
  • @minitech I know it's an *object*, but the question is whether it's JQuery, or plain Javascript. – McGarnagle Jul 03 '12 at 02:29
  • Silly questions: does your link have a `select_it` class? Is that code inside a document.ready handler? – bfavaretto Jul 03 '12 at 02:31
  • @dbaseman: It is jQuery-modified. If you need proof, look at the source, namely [this line](https://github.com/jquery/jquery/blob/master/src/event.js#L362) and [this method](https://github.com/jquery/jquery/blob/master/src/event.js#L507). – Ry- Jul 03 '12 at 02:31
  • @minitech I disagree that JQuery abstracts it. As written, you can confirm that this won't work on IE. – McGarnagle Jul 03 '12 at 02:33
  • 1
    @dbaseman: Okay, if you want to ignore the actual source code of jQuery, maybe you should just open up IE 8 and try it? – Ry- Jul 03 '12 at 02:35
  • @minitech I just did that actually.... IE follows the link. – McGarnagle Jul 03 '12 at 02:36
  • @dbaseman: I just did it too, and it doesn't. http://jsfiddle.net/minitech/MqscY/show/ – Ry- Jul 03 '12 at 02:40

1 Answers1

2

try putting the return false; out of the jQuery.post like this:

jQuery(".select_it").click(function(e){
    jQuery.post(url, function(){
        //code here   
    });
    return false;
});
eos87
  • 8,961
  • 12
  • 49
  • 77
  • This hit the point. Only returning false to the click action would prevent the link action being triggered. – BOYPT Jul 03 '12 at 04:22
  • @BOYPT: `e.preventDefault()` is called at the very beginning. – Ry- Jul 03 '12 at 13:51
  • 1
    What's more, although the formatting is admittedly very confusing, `return false;` is *already* outside of the `jQuery.post`. – Ry- Jul 03 '12 at 13:53