Reputation: 2367
I am trying to set a text to an asp label from javascript, this is what i tried but it doesnt work
document.getElementById("Label1").value = "new text value";
<asp:Label ID="Label1" name="Label1" Font-Size="XX-Large" runat="server" Text="I am just testing"></asp:Label>
Upvotes: 1
Views: 5633
Reputation: 172398
You can try this:-
document.getElementById("<%=Label1.ClientID%>").value = "new text value";
or you can try
var elMyElement = document.getElementByID('<%= Label1.ClientID %>');
elMyElement.innerHTML = "your text here";
Upvotes: 1
Reputation: 1282
Label1
is the server side ID of the Label control. Use the ClientID
to access it from the javascript. Try this:
document.getElementById("<%=Label1.ClientID%>").innerHTML= "new text value";
Hope this will help.
Upvotes: 2
Reputation: 5892
The asp.net label is rendered as a span so you need to set its innerHTML property not the value property, another option is to use JQuery and use the .text() method
Upvotes: 1
Reputation: 2325
Use..
document.getElementById('<%=Label1.ClientID%>').innerText="New Text Value" ;
Upvotes: 1
Reputation: 689
Try this document.getElementById('<%= Label1.ClientID %>').InnerHTML = "Your Text Changed";
Upvotes: 1
Reputation: 5039
ASP.NET changes "Label1" to something like MasterPageContent_Label1 when rendered to the client. Also ASP.NET Label controls are renderd to the client as <span>
elements so you need to use innerHTML as opposed to value to set the content.
document.getElementById('<%= Label1.ClientID %>').innerHTML = "new text value";
Upvotes: 3