Vervatovskis
Vervatovskis

Reputation: 2367

Editing an asp label from javascript

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

Answers (7)

Rahul Tripathi
Rahul Tripathi

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

Xmindz
Xmindz

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

Hassan Mokdad
Hassan Mokdad

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

Amol Kolekar
Amol Kolekar

Reputation: 2325

Use..

document.getElementById('<%=Label1.ClientID%>').innerText="New Text Value" ;

Upvotes: 1

Nag
Nag

Reputation: 689

Try this document.getElementById('<%= Label1.ClientID %>').InnerHTML = "Your Text Changed";

Upvotes: 1

hollystyles
hollystyles

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

Oded
Oded

Reputation: 498934

You need to get the ClientID of the control in order to manipulate it in JavaScript.

The ClientID is the Id that gets rendered in the browser.

document.getElementById("<%=Label1.ClientID%>").value = "new text value";

Upvotes: 1

Related Questions