Reputation: 26583
I have a button in my HTML page which is not part of any form.
<input type='button' id='submitter' value='add'/>
I have a click handler on it:
$('#submitter').click(function(e) {
e.preventDefault();
e.stopPropagation();
var args={}
$.get('notexists.php', args, function() {alert('what?');}, 'json')
.error(function(){ alert('error'); });
return false;
}
Now, notexists.php
does not exist, so the click should alert error
. But for some reason, The page refreshes with I click the button!
Some experiments I've tried to identify the problem:
Can anyone help?
PS: I'm using $.post in MANY other parts of this app, and its working as expected everywhere.
Upvotes: 0
Views: 3444
Reputation: 5648
I believe your selector isn't selecting the button, so the event handler is not being applied.
Test your selector by doing something like:
$("#submitter").val('Test');
Upvotes: 1
Reputation: 78667
Have you checked $('#submitter').length
when you attempt to bind the event? Maybe you have duplicate id's in the dom? Have you got your script running before the closing body tag or inside a jquery doc ready block?
Posting a fuller code sample would help to eliminate the questions I pose.
Upvotes: 1