I’ve got an ASP.Net TextBox that I want to pass it’s text value to a JavaScript function. I’ve tried several different methods, including the one at this answer, Getting Textbox value in Javascript but my value is always returned as undefined. These textboxes are part of an <asp:UpdatePanel> but I have tried this outside of the panel, and receive the same error.
This is my current code setup:
ASP.Net
<asp:TextBox ID="txtEmail" runat="server" ClientIDMode="Static"
CssClass="login-boxes" TextMode="Email" MaxLength="255" ValidationGroup="rPage1" />
<asp:TextBox ID="txtTest" runat="server" ClientIDMode="Static"
CssClass="login-boxes" ValidationGroup="rPage1" />
<asp:Button ID="btnAdvance" runat="server" CssClass="login-buttons reg-btn-show"
OnClientClick="testFunction()" UseSubmitBehavior="false" Text="Advance"
OnClick="btnAdvance_Click" ValidationGroup="rPage1" />
JavaScript
var box1 = document.getElementById('<%= txtTest.ClientID %>').value;
var box2 = document.getElementById('<%= txtEmail.ClientID %>');
function testFunction() {
box2.value = box1;
}
What I am trying to achieve, is if I type “Hello” into txtTest and click btnAdvance, it should populate txtEmail with “Hello”. How can I achieve this?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
box1 will be set at the beginning and it won’t update. So when you click btnAdvance it will not have updated value of txtTest. Try moving those box1 & box2 inside testFunction. It should work.
function testFunction() {
var box1 = document.getElementById('<%= txtTest.ClientID %>').value;
var box2 = document.getElementById('<%= txtEmail.ClientID %>');
box2.value = box1;
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0