user2958571
user2958571

Reputation: 133

change label text in asp.net

I'm trying to change label's text by using javascript:

<head runat="server">
    <script type="text/javascript">
        function updateLabel() {
            var lblElement = document.getElementbyId("Label2");
            lblElement.innerHtml("new");
        }
    </script>
</head>

I call this script from the code behind:

mainPage.ClientScript.RegisterStartupScript(GetType(), "MyKey", "updateLabel();",true);

It's not working...

Using a buildin trigger like button click won't work for me that's whyI'm trying to find a way to do it with javascript.

Upvotes: 2

Views: 19325

Answers (5)

satya prakash
satya prakash

Reputation: 97

Try This code :

$('#lblId').text('text which you want to append dynemically');

Upvotes: 0

Manoj
Manoj

Reputation: 1890

Try this code:

JS:

 <script type="text/javascript">
        function updateLabel()
          {
           document.getElementbyId("Label2").innerHTML= "new" ;
          }
    </script>

Upvotes: 0

Ajay
Ajay

Reputation: 6590

Try this

<script type="text/javascript">
   function updateLabel() {
       document.getElementById('Label2').innerHTML = 'New';
   }
</script>

Upvotes: 4

geedubb
geedubb

Reputation: 4057

Javascript is CaSe SeNsItIvE, the property is innerHTML NOT innerHtml, and it is a property not a method so you need lblElement.innerHTML = "new";. Also unless you are running .NET 4 or later and have ClientIDMode="Static" in your @page directive you will have to take into account the actual ID that is rendered in the HTML:

<head runat="server">
    <script type="text/javascript">
        function updateLabel() {
            var lblElement = document.getElementbyId('<%=Label2.ClientID%>");
            lblElement.innerHTML = 'new';
        }
    </script>
</head>

Upvotes: 0

Amit
Amit

Reputation: 15387

Try this

<head runat="server">
 <script type="text/javascript">
    function updateLabel() {
        var lblElement = document.getElementbyId("Label2");
        lblElement.innerText="new";
    }
 </script>
</head>

Upvotes: 1

Related Questions