Select inputs with number type through jQuery

Diogo Cardoso picture Diogo Cardoso · Feb 3, 2011 · Viewed 45.7k times · Source

How can I select all inputs with number type in jQuery?

The following code doesn't work:

$(':input[type="number"]').click(function () { 
   alert('hello'); 
});

Thanks.

Answer

user113716 picture user113716 · Feb 3, 2011

Your selector is correct. Using the attribute-equals-selector(docs), it will select input elements with that type.

You'll need to be sure the DOM is loaded before running it.

Example: http://jsfiddle.net/H2aQr/

$(function() {
    $(':input[type="number"]').click(function () { alert('hello'); });
});