0

I have this:

<input type="password" id="txtPassword" name="txtPassword" />

and I want to do something like this:

var password = document.getElementById("<%= txtPassword.ClientID %>").value();

but it tells me "'txtPassword' is not declared." I've tried this:

var pswd = $find('txtPassword').value();

but I get "'null' is null or not an object. So Then I tried this:

var pswd = $find('txtPassword').value();

but I got the same thing. My input control is located in this mess:

    <asp:Panel ID="pnlPassword" style="display: none" runat="server">
<div class="PasswordPopup">
            <div id="PopupHeader">&nbsp;</div>
            <div class="Controls">
                <center><table><tr>
                    <td>Please enter your password:</td><td><input type="password" id="txtPassword" name="txtPassword" /></td></tr>
                <tr><td>&nbsp;</td>
                    <td><asp:button name="btnOK" id="btnOK" runat="server" text="OK" />&nbsp;&nbsp;<asp:button id="btnCancel" runat="server" text="Cancel" /></td></tr></table></center>
            </div>
 </div>
</asp:Panel>

How can I reference that input control in my javascript function?

Thanks,

Jason

2
  • I don't see your input control in the markup. Commented Jul 12, 2011 at 21:44
  • Sorry, grabbed the wrong panel. It's there now Commented Jul 12, 2011 at 21:46

3 Answers 3

2

You need to modify your password input to runat="server" to be able to access from server script...

<input runat="server" type="password" id="txtPassword" name="txtPassword" />

Alternatively you could use an ASP.NET TextBox control with TextMode="Password"...

<asp:TextBox runat="server" id="txtPassword" TextMode="Password" />
Sign up to request clarification or add additional context in comments.

Comments

1

Server side controls (i.e. runat='server') will have generated ids when the control is rendered in the HTML, while NON server side controls will be as is.

With Quintin's solution, you should be able to use Control.ClientID to get access to your control.

The reason you were having trouble is syntax.

Solution1 (Quintin's):

  • Add runat='server' to your control
  • var password = document.getElementById("<%= txtPassword.ClientID %>").value;

Solution2 (use jQuery):

  • jQuery does not need the find notation for named elements
  • var password = $('txtPassword').value;

Solution3 (use DOM): - var password = document.getElementById('txtPassword').value;

1 Comment

solution2 was the trick. But I did vote up @Quintin and @Dmitry, thanks all for the help!
1

You don't specify runat="server" for your , so this should be enough:

  var password = document.getElementById("txtPassword").value();

If you'd like to use it in server code, add runat="server"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.