Can anyone help?
I have a change event on a textbox and a click event on a button – all html.
Now if I click the button, the click event happens – great!
But I if change something in the textbox and then click the button, the event from the Change event happens which is great but I never get the click event in the button.
This is my jQuery, is there a way around this? I am lost.
$('#myTextbox').live('change', function() {
$.ajax({
type: "POST",
url: "test.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert('the date is ' + msg.d);
}
});
});
$('#myButton').live('click', function() {
alert('i am in click');
});
EDIT
I have created a really simple html form and sure enough the click event doesn’t fire if the change fires first – actually in this example I have used blur event; but its the same:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$('#htmlbutton').bind('click', function() {
alert('i am in click');
});
$('#htmltextbox').bind('blur', function() {
alert('in blur');
});
});
</script>
</head>
<body>
<form id="form1">
<div>
<input id="htmlbutton" type="button" value="button" /><input id="htmltextbox" type="text" /></div>
</form>
</body>
</html>
It seems like your alert is squashing the button click event. If instead of alerting, you change the color of both elements, you’ll see that both events get fired.
What happens is the blur/change event gets fired as soon as your mouse button goes down on the button. Since there’s an alert, you have to go and click it off and it never registers the button up on the button, which is what you need for a click event to fire.
The ajax should have finished and gone onto the next event. Unless the call came back so quickly that it squashed the click even anyway.