1

I'm trying to set function onFocusOut event with jQuery like:

$(':number').keypress(function (evt){
    //validating cone
}

but it can't select number types throwing an error

Error: Syntax error, unrecognized expression: number

Same code for type text works fine.

Does jQuery not know a number datatype? How to make it work?

2
  • 1
    There is no such thing as :number, as it says Commented Aug 9, 2012 at 7:24
  • True, but :text is for input type="text" so i think it should be same thing for other types. Is that because number type is from HTML5? Commented Aug 9, 2012 at 7:41

2 Answers 2

5

Are you trying to select <input> elements with the number as the type? Try this:

$('input[type="number"]').keypress(function (evt){

}
Sign up to request clarification or add additional context in comments.

4 Comments

or $('input[type="number"]') which is faster.
It works, thanks. Was trying before something like $('input[type="number"]') with i found on internet bud didnt work .(possible i've made an error in syntax then...) Btw laso found examples with $('input.number]'), but it doesnt work also...
@T.G, input.number is for selecting <input/> elements with class number. Completely unrelated for what you wanted to do. You better try to understand selectors instead of just googling
one more thing. Chrome and some other browsers do validation of numeric fields as well... how to disable it? I tied $('input[type="number"]').unbind('blur'); etc, but with no effect
1

For the record, you can extend jQuery to add a custom :number selector.

$.expr[':']​​​​.number = function(elem){
  return $(elem).is("input[type='number']");
}​;

Or pure javascript.

$.expr[':']​​​​.number = function(elem){
  return (elem.tagName === "INPUT") &&
         elem.getAttribute("type") &&
         (elem.getAttribute("type").toUpperCase() === "NUMBER");
}​;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.