How can I change the HTML input type attribute with password to text?

I would like to change the "input" type attribute of an HTML element, from type = "password" to type = "text".

I know it would be easier to find an item by ID, but not every page has the same ID to enter a password. How to target password input and change their internal value to text type.

I have several lines of JavaScript code:

a=document.getElementsByTagName("input"); 
a.setAttribute(type,text);

      

+3


source to share


4 answers


To convert all elements of type password to text:

var arr = document.getElementsByTagName("input");
for (var i = 0; i < arr.length; i++) {
    if (arr[i].type == 'password') arr[i].setAttribute('type','text');
}

      



Note. Not all browsers allow dynamic attribute changes type

on inputs.

+2


source


You can get the password field using the following query.

document.querySelector('input[type="password"]').type = "text";

      



If you are dealing with multiple fields, you can use querySelectorAll

and loop through them.

var pwds = document.querySelectorAll('input[type="password"]');
for (var i=0;i<pwds.length;i++){
pwds[i].type = "text";
}

      

+1


source


This will convert each type password input to type text input for the document.

Array.prototype.slice.call(document.querySelectorAll('input[type="password"]'))
    .forEach(function (elt) {
        elt.setAttribute('type', 'text');
    });

      

+1


source


The function is called document.getElementsByTagName

(with s) and gives a list of items. So you will need to refer to it with the index ( a[0]

for the first input, etc.)

I think the problem is in determining which of the inputs you will need to change if they are not marked with IDs.

-1


source







All Articles