Monday, January 27, 2014

How to Get the ASP.NET Server Control Textbox value using JavaScript or Jquery

We can not access the server control value by directly using it's id. You need to append and prepend the <%= and %> and also the ClientID. Because when rendering in browser the server control id will be change. If you are using JQuery to get the textbox value means you need to use # symbol. Check out the sample code clearly.
To Set a value to server control check my another post Click Here
HTML Code:
<asp:TextBox ID="textbox1" runat="server"></asp:TextBox>

JavaScript Code:
var val1= document.getElementById("<%= textbox1.ClientID %>").value;

JQuery Code:
var val2= $("#<%= textbox1.ClientID %>").val();

No comments:

Post a Comment