Introduction:
Here I will explain how to get asp.net
textbox in JavaScript or get asp.net label value in JavaScript or get asp.net control values textbox, label etc in JavaScript.
Description:
In previous articles I explained jQuery display testimonials with CSS example, jQuery get or access session value, jQuery right click context menu example, jQuery Add text to end of div and many articles relating to
JQuery,
JavaScript, asp.net,
code snippets. Now I will explain how to get asp.net
textbox or label value in JavaScript.
Get
Asp.net Textbox or Label Value in JavaScript
To get asp.net
textbox or label value in JavaScript
we need to write a code like as shown below
Get
label value
var amount = document.getElementById("<%=lblAmount.ClientID
%>").innerHTML
Get
label value
var name = document.getElementById("<%=txtUserName.ClientID
%>").value;
|
If you want to see it in
complete example check below code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Get Asp.net Textbox Value in JavaScript</title>
<script type="text/javascript">
function getvalues() {
var name = document.getElementById("<%=txtUserName.ClientID
%>").value;
var amount = document.getElementById("<%=lblAmount.ClientID
%>").innerHTML;
alert("Textbox
Value: "+name+"\n" +"Label Value: "+ amount);
return false
}
</script>
</head>
<body>
<form id="form1"
runat="server">
<div>
UserName:<asp:TextBox ID="txtUserName" runat="server" /><br />
Amount:<asp:Label ID="lblAmount" runat="server" Text="10000"/><br
/>
<asp:Button ID="btnValidate"
runat="server"
Text="Get
Values" OnClientClick="javascript:getvalues();" />
</div>
</form>
</body>
</html>
|
Live
Demo
To test live example click on below button
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
6 comments :
Hi Suresh,
Some error in heading pls check,
Get label value
var amount = document.getElementById("<%=lblAmount.ClientID %>").innerHTML
Get textbox value
var name = document.getElementById("<%=txtUserName.ClientID %>").value;
@manoj kumar: nice. He used "label" again instead of "textbox".
its not working in Master's Content pages Why
@manoj Kumar :put a semicolon(;) after .innerHtml of ur code
I am extracting the date from a textbox in the form dd-mm-yyyy(type is DateTime) and time from another textbox but
in the form of hh:mm:ss(type is TimeSpan)and concating both the extracted values to store in a variable of type "DateTime".
So, my final datetime variable stores a value in the form of dd-mm-yyyy hh:mm:ss.The problem is the textbox values are in
hh:mm form but its taking the valus in hh:mm:ss form due to the type TimeSpan.
But i want the value in the form of dd-mm-yyyy hh:mm.
Hows to do it???
e.g.
DateTime finalvalue= DateTime.parse(txtDate.text)+ " " +TimeSpan.Parse(txtTime.text);
not work
Note: Only a member of this blog may post a comment.