T
T
t0os2011-11-18 11:57:43
JavaScript
t0os, 2011-11-18 11:57:43

How to assign 2 handlers at once in jQuery: blur + keydown?

Sample code here: jsfiddle.net/8Jfne/29/

The bottom line is this: there is a set of inputs. When moving over them (using the keyboard or mouse), when leaving them, the value is checked. If not numerical, then we swear and return to editing the problematic input.

And now the problem:
In Chrome, if you enter a non-numeric value into the input and press Enter, then it works onkeydown - everything is fine. In FF and IE, both handlers work, so the error message appears twice (in FF, I generally have it three times, onblur works 2 times).

I would like to ask how to get out of this situation?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
Vitaly Zheltyakov, 2011-11-18
@VitaZheltyakov

Check if error message.

M
max_rip, 2011-11-18
@max_rip

Instead of it is simpler to hang up event of check on onsubmit?

V
Vitali Borovik, 2011-11-18
@WAYS

Here is jsfiddle.net/swbEn/1/

S
sdevalex, 2011-11-18
@sdevalex

So it doesn't roll?

$(...).on('blur keydown', function(){
    //...
});

S
sdevalex, 2011-11-18
@sdevalex

$(function() {
     function isInt(x) { 
         var y = parseInt(x); 
         if (isNaN(y)) return false; 
         return x == y && x.toString() == y.toString(); 
     } 
     
     $('input').on('blur keydown', function(){
         if(!isInt($(this).val()))
             $(this).focus();
     });
});  

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question