I have an input box, and I have bound both blur and keypress events to the input, but the problem is when there is a keypress event, blur event also fires. Is there any way to suppress the blur event when keypress event occurs?
-
It'd help if you posted an example of your code. I don't think blur should be firing from keypress unless your keypress handler is actually causing the element to lose focus.David– David2009-02-01 01:32:08 +00:00Commented Feb 1, 2009 at 1:32
-
Don't use the keypress event, use the keyup event instead (better supported)James– James2009-02-01 01:34:30 +00:00Commented Feb 1, 2009 at 1:34
3 Answers
I simplified your code a little just to test and couldn't come to the same conclusion about the order of events firing. Firebug's console shows the following code execution yielding .keypress always occuring before a .blur
<script>
$(document).ready(function() {
$("#inpt").blur(function(e) {
console.log(".blur");
});
$("#inpt").keypress(function(e) {
console.log(".keypress");
});
});
</script>
What exactly are you trying to accomplish with .onchange, and .keypress bound to the same field? Might help if you posted your event handler code as well.
Also, you have a global document.keypress bound as well, any particular reason for that?
Comments
<script src="jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#inpt").blur(function(e) {
EventHandlerBlur(e);
});
$("#inpt").keypress(function(e) {
EventHandlerKeyPress(e);
});
$(document).keypress(function(e) {
EventHandlerKeyPress(e);
});
});
</script>
here is the code with same behavior. actually on entering on input/ or even pressing any key on input box is causing blur event to fire first before the keypress event..