tags:

views:

175

answers:

5

Hi.

I'm new in javascript. I want to get all input type password on my html page.

I know that there is a way to do this kind off things using Javascript, but I don't know how.

Then, with each one, I want to assign an event on text changed.

How can I do this?

Thanks

+6  A: 

I presume 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;
}
Robusto
+1 for using ===.
Marcel Korpel
@Marcel Korpel what difference does it make in this case? If `inputs[i].type` is null this would have already thrown an exception, and if it is not a String then `toLowerCase` would not exist, also throwing an exception. If `inputs[i].type` is a String then `toLowerCase` will always return a String.
Renesis
I hope you don't mind, I extended your answer to use `querySelectorAll` if it's available, it saved me valuable seconds off writing my own :-)
Andy E
@Renesis: yeah, you're right. It doesn't matter in this case. Though I think it's a good habit.
Marcel Korpel
@Andy E: I don't mind at all. See my comment @ your post.
Robusto
A: 

Hi, This is untested, but something like this should work:

var allElem = document.getElementsByTagName(’input’)

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

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

              allElem[i].onchange = //<your onchange function>
          }    
}
keyboardP
+3  A: 

You can do this easily using jQuery:

jQuery("input[type='password']").change( function() {
  // check input ($(this).val()) for validity here
});
Justin Ethier
To the OP: If you don't mind using jQuery, this solution will be the best option for you, since it has built-in event handling that won't clobber events already on the object. If you simply set `.onchange`, then you break existing event handlers, and if you want to work around it, you will have to implement your own way to store the original event handlers (results in ugly code unless you already have a system for this).
Renesis
+1  A: 

jQuery is your friend here. With jQuery, it's as easy as using the selector:

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

and then binding a changed listener to each:

$('input[type=password]').change(function ()
{
    // do whatever here
});
Matt Ball
+2  A: 

I hope Robusto doesn't mind me extending his solution a bit for a solution that will perform better on the modern browsers. Chrome, Safari, IE8 and Firefox all support querySelectorAll, so it seems more appropriate to use that if it's available.

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

  // If not, use Robusto's 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. It shouldn't be a problem but it might be worth noting that querySelectorAll will return a collection, whereas the fallback method will return an array. Still not a big deal, they both have the length property and there members are accessed the same way.

Andy E
No, I don't mind at all Andy. Any improvements are always welcome. Where I work we still have to support IE 6, so I fall into lowest-common-denominator coding habits. But you're right, document.querySelectorAll, if available, is more economical.
Robusto