Click here to Skip to main content
13,192,855 members (54,877 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
i have a textbox and a label..when user writes anything in textbox i need the text in textbox to be immediately updated in label..how to achieve this using javascript?this is my code.and it is not working

<asp:TextBox id="txtbox1" runat="server" onchange="settext()">
<asp:Label ID="Label6" runat="server"ForeColor="White" Width="200px"></asp:Label>


<script>
 function settext()
{
    var name = document.getElementById('<%= txtbox1.ClientID %>').value;
    document.getElementById("Label6").innerHTML = name;
}
</script>
Posted 13-Jun-14 0:05am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Try this:
<script>
        function settext(e) {
            document.getElementById("<%= Label1.ClientID %>").innerHTML = e.value;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" onkeyup="settext(this);" onkeydown="settext(this);"></asp:TextBox>
        <asp:Label ID="Label1" runat="server" Text="Label">dd</asp:Label>
    </div>
    </form>
</body>
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

There are two ways for it::

1st one is:

<asp:label id="Label6" runat="server" forecolor="White" width="200px" xmlns:asp="#unknown"></asp:label>


<script>
 function settext()
{
    var name = document.getElementById('<%= txtbox1.ClientID %>').value;
    document.getElementById('<%=Label6.ClientID%>').innerHTML = name;
}
</script>



2nd one is


<asp:label id="Label6" runat="server" forecolor="White" width="200px" ClientIdMode="Static" xmlns:asp="#unknown"></asp:label>


<script>
function settext()
{
    var name = document.getElementById('&lt;%= txtbox1.ClientID %&gt;').value;
    document.getElementById('Label6').innerHTML = name;
}
</script>
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

try onkeypress event of textbox.. :)

function settext(valueData)
{
  
    document.getElementById('<%=Label6.ClientID%>').innerHTML = valueData;
}


HEML

<asp:textbox id="txtbox1" runat="server" onkeypress="settext(this.value)" >
</asp:textbox>
  Permalink  
v2
Comments
Debabrata_Das 13-Jun-14 6:35am
   
my 5!
Nirav Prabtani 13-Jun-14 6:43am
   
thank you.. :)
Naga Sindhura 16-Jun-14 3:33am
   
Sorryy to ask now,
The output i am getting here is one char back,
like if I enter "Hello", In my label it is displaying as "Hell".
How can I solve this.
Nirav Prabtani 16-Jun-14 9:31am
   
Post your code , according to my solution it should not happen.
Naga Sindhura 17-Jun-14 1:00am
   
Hi Nirav, Here is my code,
<head runat="server">
<title></title>
<script type="text/javascript">
function settext(valueData) {

document.getElementById('<%=Label6.ClientID%>').innerHTML = valueData;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>text </p>
<asp:textbox id="txtbox1" runat="server" onkeypress="settext(this.value)" >

<asp:Label ID="Label6" runat="server">

</div>
</form>
</body>
</html>
faizel s 20-Jun-14 3:06am
   
the code works well but when user clicks on the enter key,the page refreshes and all the data will be lost... :( how to tackle it?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

document.getElementById("Label6").innerHTML = name;
}



you are using asp label ,so use same method ,ie, document.getElementById('<%=Label6.ClientID%>').innerHTML to assign data.....
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.171017.2 | Last Updated 13 Jun 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100