简体   繁体   中英

How to change the HTML input type attribute from 'password' to' text'?

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

I am aware that it would be easier to find element by ID but not every page has the same id for the password input. How do I target the password inputs and change their inner value to a type of text.

I have this few lines of JavaScript code:

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

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 changes to the type attribute on inputs.

You can get password field using the following query.

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

If you're dealing with more than one field, 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";
}

This transforms every input of type password into input of type text for a document.

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

The function is called document.getElementsByTagName (with an s) and it yields a list of elements. So you will need to address it with an index ( a[0] for the first input, etc.)

I think the problem is determining which of the inputs you will need to change though, if they're not marked with IDs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM