Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mask and unmask input text in html

I have few text fields like SSN , phone number and email id. I would like to do something like onfocus it should display the whole content of that input box and on blur it should mask the content back to something (say asterix).Thanks in advance

like image 701
user1482239 Avatar asked Jun 26 '12 09:06

user1482239


1 Answers

You could switch the type of the <input> element between password and text in both events. See this example fiddle.

HTML

<input type="password" id="target" />

JS

<script>
var inp = document.querySelector( '#target' );

inp.addEventListener( 'focus', function(){
    inp.type = 'text';
});
inp.addEventListener( 'blur', function(){
    inp.type = 'password';
});
</script>
like image 119
Sirko Avatar answered Sep 21 '22 03:09

Sirko