Click here to Skip to main content
15,666,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to assign value to label at runtime using onkeypress event. For this I wrote the following java script:
function Keypress()
    var keyId=document.getElementById('ctl00_ContentPlaceHolderTM_txtTitle').value;
  alert(keyId);        document.getElementById('ctl00_ContentPlaceHolderTM_lblAddTitle')=keyId;

This java script works on key press event but not assigns a value to the label. Why?
Updated 27-Jan-11 22:03pm

Try something along the lines of:

document.getElementById("ctl00_ContentPlaceHolderTM_lblAddTitle").innerText = keyId;
Share this answer
Sandeep Mewara 28-Jan-11 4:38am    
or innerHTML property to assign the value.
maya saste 28-Jan-11 4:38am    
I try this way but value is not assign to label.
sapien4u 1-Feb-11 4:17am    
innerText will work but it will be reset on postback.
Try with my answer posted below.
Try this

document.getElementById('ctl00_ContentPlaceHolderTM_lblAddTitle').innerHtml = keyId
Share this answer
sapien4u 1-Feb-11 4:16am    
innerHtml is not working in my case
jagadesh437 21-Feb-13 4:11am    
innerHtml and innerText both are working fine but my Label properties are changing
The issue is, when we try to set value to the label from javascript, at first it will accept the new value but on post back, it will be changed to its previous value.

But onkeypress event, it will work untill there is a post back.
When we call a return false to avoid postback on button click the value will stay untill the next postback.

Please try with the following code (It is working, but resets on postback):

Use the following JavaScript functions:
function ChangeText(retval) {
            document.getElementById("testLabel").innerText = "New Text (onclick) : Return Value - " + retval;
            return retval;
        function keyPress() {
            document.getElementById("testLabel").innerText = "New Text (onkeypress)";

Add the following controls to test the function:
<asp:Label ID="testLabel" runat="server" Text="ASP LABEL CONTROL"></asp:Label>
        <input type="button" value="HTML btn retval true" onclick="ChangeText(true);" />
        <input type="button" value="HTML btn retval false" onclick="ChangeText(false);" />
        <asp:Button ID="testButton" runat="server" Text="ASP btn retval false" OnClientClick="javascript:return ChangeText(false);" />
        <asp:Button ID="testButton2" runat="server" Text="ASP btn retval true" OnClientClick="javascript:return ChangeText(true);" />
        <asp:TextBox ID="testTextBox" runat="server" onkeypress="javascript:keyPress();"></asp:TextBox>

Please feel free to revert back after testing and add your comments on it. :)
Share this answer
bharat peggem 10-May-12 6:57am    
How can I send a value from textbox to label using html?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900