Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select inputs with number type through jQuery

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.

like image 890
Diogo Cardoso Avatar asked Feb 03 '11 14:02

Diogo Cardoso


2 Answers

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'); });
});
like image 64
user113716 Avatar answered Oct 01 '22 17:10

user113716


You don't need the colon at the beginning, that's for selecting types of input, like $('input:text')

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

works just fine

like image 37
EvilAmarant7x Avatar answered Oct 01 '22 18:10

EvilAmarant7x