Password selector

The selector :password selects elements of type password. See password inputs. The equivalent of $(':password') is $('[type=password]'). Since :password is not a CSS specification, for better performance in modern browsers it is better to use [type='password'] instead.

Syntax

So we select fields with the type password:

javascript$(':password');

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. $(':password') will be treated as $('*:password'), so it is better to use $('input:password') instead.

Example

Let's select all inputs with type password 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="password">
  <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:password').css({background: 'green', border: '2px red solid'}); $('form').submit(function(event) { event.preventDefault(); // prevents form submission });

<

-javascript->

See also

  • tag password
  • method css,
    which allows you to get and change the CSS styles of an element
  • method find,
    which searches for elements within those already found