Input selector
The :input
selector selects form controls - inputs, textareas, dropdowns, and buttons. See the tags: input
, textarea
, button
, select
. Since :input
is not part of the CSS specification, for better performance in modern browsers it is better to first filter the elements using a pure CSS selector and then apply .filter(':input')
.
Syntax
This is how we select form controls:
$(':input');
Example
Let's, according to the theory above, select all form controls and output their number to the console, using the length
property. We'll see that all 13
tags are there:
<form>
<input type="button" value="button">
<input type="checkbox">
<input type="file">
<input type="hidden">
<input type="image">
<input type="password">
<input type="radio">
<input type="reset">
<input type="submit">
<input type="text">
<select>
<option>option</option>
</select>
<textarea></textarea>
<button>button</button>
</form>
textarea {
height: 25px;
}
let allInputs = $(':input');
console.log('Tags amount: ' + allInputs.length);
$('form').submit(function(event) {
event.preventDefault(); // prevents form submission
});