Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 938
A K Avatar asked Oct 23 '25 17:10

A K


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

like image 180
AlliterativeAlice Avatar answered Oct 26 '25 07:10

AlliterativeAlice


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');
    });
like image 24
axelduch Avatar answered Oct 26 '25 07:10

axelduch