tags:

views:

54

answers:

2

this problem is so weird.

i have used jquery to link a specific id to an alert("hello").

$("#submit_reply").live("click", function() {
    event.preventDefault();
    alert("hellooooo");
});

now in safari when i click it works. but when im using firefox its not working. the submit_reply (a submit button) refreshes the whole page. that is to say, jquery wasnt able to bind it.

how can i debug this problem? firefox shows nothing wrong in console. but why did it work in safari? what tools can i use to check what the problem is...help!

+5  A: 

You are not passing in the event object to the function.

Try this:

$( "#submit_reply" ).live( 'click', function( e ) {
     e.preventDefault();
     alert( 'hello' );
} );

Or this:

$( "#submit_reply" ).live( 'click', function( ) {
     alert( 'hello' );
     return false;
} );

As long as e.preventDefault() or return false; are executed before the end of the function, you're good to go.

Jacob Relkin
what does the return false do?
never_had_a_name
it's equivalent to calling preventDefault on the passed event.
spender
Actually is equivalent to call both, `preventDefault` and `stopPropagation`.
CMS
+1  A: 

The problem is probably that you never send in the event parameter into the function, so the first line in your click handler fails and Firefox resorts to the original click action - follow the link.

Try this change:

$('#submit_reply').live('click', function(ev) {
    ev.preventDefault();
    alert('hello, is there anyone home?');
});
Tomas Lycken
but why did it work in safari?
never_had_a_name
Probably because safari didn't stop running the javascript just because one line fails. I'm no expert on how safari handles javascript links, but in Firefox any javascript that fails (for example trying to call a function on an undefined variable) will and nothing more will happen. Maybe Safari just goes to the next line and hopes for the best.
Tomas Lycken