Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to implement show and hide functionality for textbox Password,I have done that with the help of jquerr it is working properly when i click on show password first time but when i am clicking second time on show password checkbox,autometically the show password checkbox goes disappear,please help me with this problem

What I have tried:

ASP.NET
<div class="form-group">
                          <asp:Label ID="lblPassword" runat="server" Text="Password" Font-Bold="true"></asp:Label>
                            <asp:TextBox ID="txtPassword" runat="server"  TextMode="Password" CssClass="form-control" placeholder="Enter Password Here" ></asp:TextBox>
                           <label for="chkShowPassword">
                             <input type="checkbox" id="chkShowPassword" />
                                      Show password</label>


HTML
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>



$(function () {
debugger;
$("#chkShowPassword").bind("click", function () {
var txtPassword = $("[id*=txtPassword]");
if ($(this).is(":checked")) {
txtPassword.after('<input onchange = "PasswordChanged(this);" id = "txt_' + txtPassword.attr("id") + '" type = "text" value = "' + txtPassword.val() + '" />');
txtPassword.hide();
} else {
txtPassword.val(txtPassword.next().val());
txtPassword.next().remove();
txtPassword.show();
}
});
});

function PasswordChanged(txt) {
$(txt).prev().val($(txt).val());
}
Posted
Updated 21-Dec-16 4:33am
v2
Comments
gggustafson 21-Dec-16 10:36am    
You don't need jQuery for this process. When the visitor checks the "Show password" CheckBox, just change TextMode="Password" to TextMode="SingleLine". When the visitor clears the Checkbox, change the TextMode="SingleLine" back to TextMode="Password".

1 solution

That checkbox will always disappears on unchecked because of this:
txtPassword.next().remove();

as it happens to be the next element after the password textbox.
 
Share this answer
 
Comments
ravitv 16-Aug-21 3:42am    
Hi Peter Leow,
Can you please update on your Solution1 code, I dont know how to implement your code.
It keeps adding more and more textboxes and checkboxes.

txtPassword.next().remove();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900