开发者

Show second textbox on keypress

开发者 https://www.devze.com 2023-01-21 00:10 出处:网络
I have two textboxes (pass1 and pass2).开发者_JS百科 I want for pass2 to be hidden and only appear when something is entered in pass1. Pass2 should hide when pass1 is null.Use a function that syncs pa

I have two textboxes (pass1 and pass2).开发者_JS百科 I want for pass2 to be hidden and only appear when something is entered in pass1. Pass2 should hide when pass1 is null.


Use a function that syncs pass2 with pass1.

function syncTextFields() {
    var pass1 = this;
    var pass2 = document.getElementById('pass2');

    if(pass1.value == '') {
        pass2.style.display = 'none';
    }
    else {
        pass2.style.display = '';
    }
}

Then run the sync function when anything is typed in the pass1 field.

pass1.onkeyup = syncTextFields;

We separated the logic into a function so that function can be run separately on page load as well.

window.onload = function() {
    syncTextFields();
    document.getElementById('pass1').onkeyup = syncTextFields;
};


<script type="text/javascript">

function checkVisibility() {

   var pass1 = document.getElementById('pass1');
   var pass2 = document.getElementById('pass2');

   if (pass1.value.length > 0) {
      pass2.style.visibility = 'visible';
   } else {
      pass2.style.visibility = 'hidden';
   }
}

</script>

<input id="pass1" type="password" onkeyup="checkVisibility()" />
<input id="pass2" type="password" style="visibility: hidden;"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消