Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if input is textbox, select, textarea or radio

Tags:

jquery

I have a form with different html input fields...

1) <input type="text">
2) <textarea></textarea> 
3) <input type="checkbox">
4) <input type="radio">
5) <select></select>

How would I be able to determine what type of input field it is using jQuery. For example: If I wanted to do a check to see if input = "select" then do stuff.

like image 564
Joe Avatar asked Jun 26 '12 20:06

Joe


1 Answers

$('input') // selects all types of inputs
$('input:checkbox') // selects checkboxes
$('select') // selects select element
$('input:radio') // selects radio inputs
$('input[type="text"]') // selects text inputs

you can use event.target.type, this alerts what type of input, textrea or select is the target of the event.

$('input, textarea, select').change(function(event){
   alert(event.target.type)
})

http://jsfiddle.net/Q4BNH/

like image 88
undefined Avatar answered Oct 28 '22 02:10

undefined