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:

javascript$(':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:

html<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>
csstextarea {
  height: 25px;
}
javascriptlet allInputs = $(':input');
console.log('Tags amount: ' + allInputs.length);

$('form').submit(function(event) {
  event.preventDefault(); // prevents form submission
});

See also

  • method filter,
    which filters elements in a set by a given selector
  • tag form
  • tag input