I think this is what you want. Please see this.
function toggle(button) {
var password = document.getElementById("password");
if (password.type == "password") {
button.innerHTML = "Hide";
password.type = "text";
}
else {
button.innerHTML = "Show";
password.type = "password";
}
}
<input type="password" id="password" />
<button class="ui-component__password-field__show-hide" type="button" onclick="toggle(this);">Show</button>
What does this code does?
The html code creates the input as well as the button with Show
written inside it. When the button is clicked, the onclick attribute is triggered which in turn calls a function of javascript named toggle
. I had added this
in the attribute's value inside ()
and had changed it to button
inside the js part which helped me to create a variable named button
whose value was the button. Now lets come back to the javascript part. When the function in javascript is called, it first creates a variable named password
whose value is the input field. Then, it checks whether the type
of the input field
is password
or text
If it finds that its type is password
, then it changes the button's value(text written inside it) to Hide and also changes the type of the input field to text. If it finds any other type of the input fiels i.e. if it finds that the type of the field is text, it changes the value of the button to Show and the field's type to password.
Also, check this out by clicking here.