jQuery clear input[type=password]

26,884

Solution 1

Try with

$("input[type='password']").val('');

Solution 2

Try using the :password selector as it will make your code shorter:

$(':password').val('');

Also your initial code should work. Your problem is somewhere else. Unfortunately as you haven't stated in what context and how this is called or as you haven't explained what it doesn't work mean I cannot help you further.

Solution 3

short answer :

to select all password fields using jquery you can use :

$(':password').val('');

but for better performance in modern browsers use

$("input[type='password']").val('');

Detailed Answer

:password Selector ( from jQuery document )

Description: Selects all elements of type password.

version added: 1.0

jQuery( ":password" )

$( ":password" ) is equivalent to $( "[type=password]" ).

As with other pseudo-class selectors (those that begin with a ":") it is recommended to precede it with a tag name or some other selector; otherwise, the universal selector ( "*" ) is implied.

In other words, the bare $( ":password" ) is equivalent to $( "*:password" ), so $( "input:password" ) should be used instead.

Additional Notes:

Because :password is a jQuery extension and not part of the CSS specification, queries using :password cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use [type="password"] instead.

Share:
26,884
user398341
Author by

user398341

Updated on July 24, 2020

Comments

  • user398341
    user398341 almost 4 years

    I'm trying to clear all the inputs that are password type when jquery validation fails - I tried the following (which doesn't work) :

    $('input[type=password]').val('');
    

    Any ideas?

    • Thomas Shields
      Thomas Shields almost 13 years
      works for me so perhaps there's something in your markup or some other code interfering.
  • user398341
    user398341 almost 13 years
    yes- sorry guys - it was in my code - sorry for unnecessary ticket - I've already flagged it as to be removed.
  • WEFX
    WEFX over 11 years
    That will only clear one specific field. The question asks how to clear ALL password fields at once.