Show plain text as watermark for password text box using jQuery

Declare two textbox, one is simple and other one with password mode.

1 <input type="text" class="greyText" id="txtPlainPassword"/>
2 <input type="password" id="txtPassword" />

Now when my document.ready() function gets called, hide the txtPassword text box and show the txtPlainPassword text box.

1 $("#txtPlainPassword").show();
2 $("#txtPassword").hide();

Now when the focus moves to the txtplainPassword text box, hide this text box and show the txtPassword text box.

1 $("#txtPlainPassword").focus(function() {
2   $(this).hide(); 
3   $("#txtPassword").show();
4   $("#txtPassword").focus();
5 });

Now on blur event of txtPassword, first check whether anything entered or not. If not entered then txtPlainPassword text box should be displayed again. If anything was entered then there is no need to show the watermark.

1 $("#txtPassword").blur(function() {
2     if($(this).val().length == 0)
3     {
4        $(this).hide(); 
5        $("#txtPlainPassword").show();
6     }     
7 });
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s