Set default value of a password input so it can be read

前端 未结 5 1986
小鲜肉
小鲜肉 2020-12-11 16:42

I want to have a password field which says \"Password\" in it before a user enters their password (so they know what the field is)

I\'d rather just use Javascript, i

5条回答
  •  甜味超标
    2020-12-11 17:20

    Add the following code to your javascript:

    function makePasswordHidden() {
        document.getElementById("password").setAttribute("type", "password");
    }
    

    Change the input for the password to:

    
    

    What this does it it makes the input element a 'password' and the value is updated so that it is hidden. If you would like for the value="Password" to be visible if the field is left empty then add the following javascript function:

    function makePasswordShown() {
        document.getElementById("password").setAttrivute("type", "text");
    }
    

    And add the following to your password input:

    onblur="if (this.value=='') makePasswordShown(); if (this.value=='') this.value='Password';"
    

提交回复
热议问题