Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
166
rated 0 times [  170] [ 4]  / answers: 1 / hits: 17846  / 9 Years ago, wed, june 3, 2015, 12:00:00

I tried all the possible solutions to get the value of an element by id, but it did not work. I am using ASP.NET, I have searched for the controls and I know that the server changes the id of the TextBox1, so we use the clientID. But when I write console.log(data1), I get nothing or empty space.



var data1 = document.getElementById('MainContent_TextBox1').textContent;
var data1 = document.getElementById(<%=MainContent_TextBox1.ClientID %>).value;


This is the ASPX code:



<asp:TextBox ID=TextBox1 runat=server ></asp:TextBox>


And this the JS code:



 var data1 = document.getElementById('MainContent_TextBox1').textContent;


In the console, I get this error:




Uncaught SyntaxError: Unexpected end of input and its reference in the master file!




console.log(data1); appears in the console as empty place.



If anyone knows another way or why it is not working, please tell me.


More From » asp.net

 Answers
14

Id of server controls gets changed and is appended with content place holder id so a very easy solution is to set a property ClientIDMode=static on server controls. By setting this id of control will remain same and will not get changed so you will find it by getElementById in javascript.



<asp:TextBox ID=TextBox1 runat=server ClientIDMode=static ></asp:TextBox>

[#66347] Tuesday, June 2, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
efrainjamiry

Total Points: 234
Total Questions: 110
Total Answers: 112

Location: French Southern and Antarctic Lands
Member since Fri, Jan 6, 2023
1 Year ago
;