views:

971

answers:

4

hi, 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 blur event when keypress event occurs?

thanks

A: 

<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..

jyotishka bora
A: 

i think i found a simple solution.. to handle onchange event instead of blur.

but that still doesnt explain the weird behavior

jyotishka bora
+1  A: 

It sounds like something in your EventHandlerKeyPress() function is raising a blur.

Dave Ward
+1  A: 

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?

David Mosher