why this javascript is not working [Resolved]

Posted by Rickeybglr under JavaScript on 9/11/2012 | Points: 10 | Views : 1857 | Status : [Member] | Replies : 7
i have to set the label text as the value entered in textbox ..but ths script is not working whr i am doing wrong

<script type="text/javascript">
function getVal() {

document.getElementById('lbl').innerText = document.getElementById('TextBox1').value;
};
</script>

<div>
<asp:Label ID="lbl" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="getVal" />
</div>




Responses

Posted by: Samirg on: 9/11/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
The problem is with the way the label and textbox is accessed by Javascript. Try this,
function GetVal() {
var lblValue = document.getElementById('<%=lbl.ClientID %>');
var txtValue = document.getElementById('<%=TextBox1.ClientID %>');
lblValue.innerText = txtValue.getAttribute('value');
return false;
};

<asp:Label ID="lbl" runat="server" Text=""></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick=" return getVal()" />

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Samirg on: 9/11/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
because the 'return false' statement is missing

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Maheshvishnu on: 9/11/2012 [Member] Starter | Points: 25

Up
0
Down
I think may be problem with the button code

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="getVal()" />


mahesh

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tanujad on: 9/11/2012 [Member] Starter | Points: 25

Up
0
Down
The problem is with the way you are calling the javascript function on client click of button.
Use OnClientClick="javascript:return GetVal();"

Refer the following code snippet --

<script type="text/javascript">

function GetVal() {
document.getElementById('lbl').innerText = document.getElementById('TextBox1').value;
var stringvlaue = document.getElementById('lbl').innerText
alert(stringvlaue);
}
</script>

<div>
<asp:Label ID="lbl" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:return GetVal();" />
</div>


Thanks
Tanuja
(If this content helped you, Please mark it as Answer)

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rickeybglr on: 9/11/2012 [Member] Starter | Points: 25

Up
0
Down
hi tanujad,

hey now my javascript is callng.. label is not able to hold the value for longer time. whn i click on button label changed but retain its default value just aftr milli sec..why ths??

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rickeybglr on: 9/11/2012 [Member] Starter | Points: 25

Up
0
Down
thnks samir..its working as expected

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rambabu123 on: 9/12/2012 [Member] Starter | Points: 25

Up
0
Down
<script type="text/javascript">
function getVal() {

document.getElementById('lbl').innerText = document.getElementById('TextBox1').value;
var lblText=document.getElementById('lbl').innerText;
alert('lblText');
return false;
};
</script>

<div>
<asp:Label ID="lbl" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return getVal();" />
</div>

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response