Selector text

The selector :text selects inputs with the type text. See the tag input. Since :text is not part of the CSS specification, for better performance in modern browsers it is better to use [type='text'] instead.

Syntax

This is how we select inputs with type text:

javascript$(':text');

As with other pseudo-class selectors (those starting with ':'), it is better to precede ':' with the name of the tag or another selector, otherwise the selector '*' will be applied, i.e. $(':text') will be treated as $('*:text'), so it is better to use $('input:text') instead. It is important to remember that :text will also select all inputs that do not have the type attribute specified, you can see the difference in behavior in the following example:

javascript$('<input>').is('[type=text]'); // will return false
$('<input>').is(':text'); // will return true

Example

Let's select all inputs with type text and give them a green background and a red border using the css method:

html<form>
  <input type="button" value="button">
  <input type="file">
  <input type="checkbox">
  <button>button</button>
  <input type="reset">
  <input type="radio" name="test">
  <input type="radio" name="test">
  <input type="checkbox">
  <input type="text">
</form>
javascript$('form input:text').css({background: 'green', border: '2px red solid'});

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

See also

  • tag input
  • selector input,
    which selects form controls - inputs, textareas, drop-down lists and buttons
  • method filter,
    which filters elements in a set by a given selector