Get all input type password

I am new to javascript. I want to get all input type password in my html page.

I know there is a way to do this using Javascript, but I don't know how to do it.

Then, with each of them, I want to assign an event with the changed text.

How can i do this?

thank

+5
source share
5 answers

I guess you mean

<input type="password">

If so, you can try a function like this:

function getPwdInputs() {
  var ary = [];
  var inputs = document.getElementsByTagName("input");
  for (var i=0; i<inputs.length; i++) {
    if (inputs[i].type.toLowerCase() === "password") {
      ary.push(inputs[i]);
    }
  }
  return ary;
}
+11
source

, , , . Chrome, Safari, IE8 Firefox querySelectorAll, , .

function getPwdInputs() 
{ 
  // If querySelectorAll is supported, just use that!
  if (document.querySelectorAll)
    return document.querySelectorAll("input[type='password']"); 

  // If not, use Robusto solution
  var ary = []; 
  var inputs = document.getElementsByTagName("input"); 
  for (var i=0; i<inputs.length; i++) { 
    if (inputs[i].type.toLowerCase() === "password") { 
      ary.push(inputs[i]); 
    } 
  } 
  return ary; 
} 

NB. , , , , querySelectorAll , . , length, .

+4

, jQuery:

jQuery("input[type='password']").change( function() {
  // check input ($(this).val()) for validity here
});
+3

jQuery - . jQuery , :

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

changed :

$('input[type=password]').change(function ()
{
    // do whatever here
});
+1

, - :

var allElem = document.getElementsByTagName(’input’)

for (i=0; i < allElem.length; i++) {

          if (allElem[i].getAttribute(’type’)==‘password’) {

              allElem[i].onchange = //<your onchange function>
          }    
}
0

All Articles