Avatar of fwstealer
fwstealerFlag for United States of America

asked on 

set label value with javascript

i can get everything but the setting of the label.

function TotalUsedShares07()
{
    var _txtBox1 = document.getElementById('<%= txtbx07d.ClientID %>').value;            
    var _txtBox2 = document.getElementById('<%= txtbx07e.ClientID %>').value;
    var _txtBox3 = document.getElementById('<%= txtbx07f.ClientID %>').value;
   
    var value1 = _txtBox1;            
    var value2 = _txtBox2;  
    var value3 = _txtBox3;
         
    document.getElementById('<%=lbl07g.ClientID %>').value = parseFloat(value1) + parseFloat(value2) + parseFloat(value3);
}
ASP.NETJavaScript

Avatar of undefined
Last Comment
Knakworst
Avatar of Ovid Burke
Ovid Burke
Flag of Saint Vincent and the Grenadines image

Assuming that 'label' in not a form field, you can try:
document.getElementById('<%=lbl07g.ClientID %>').innerHTML = parseFloat(value1) + parseFloat(value2) + parseFloat(value3); 

Open in new window

Avatar of fwstealer
fwstealer
Flag of United States of America image

ASKER

its in a form - part of a table; i tried the following:

function TotalUsedShares07()
{
    var _txtBox1 = document.getElementById('<%= txtbx07d.ClientID %>').value;            
    var _txtBox2 = document.getElementById('<%= txtbx07e.ClientID %>').value;
    var _txtBox3 = document.getElementById('<%= txtbx07f.ClientID %>').value;

    var value1 = _txtBox1;            
    var value2 = _txtBox2;
    var value3 = _txtBox3;
    var valSum = parseFloat(value1) + parseFloat(value2) + parseFloat(value3)
    alert(valSum);

    var elMyElement = document.getElementById('<%= lbl07g.ClientID %>').value;
    alert(elMyElement); //returns nothing
    document.getElementById("lbl07g").innerHTML = valSum; //fails setting value
    alert(document.getElementById('<%= lbl07g.ClientID %>').value);
    //elMyElement.innerHTML = valSum;
    //document.getElementById('<%=lbl07g.ClientID %>').value = valSum;
}
Avatar of Ovid Burke
Ovid Burke
Flag of Saint Vincent and the Grenadines image

How do you call or trigger function TotalUsedShares07() ? Can you show me the HTML code?
Avatar of Ovid Burke
Ovid Burke
Flag of Saint Vincent and the Grenadines image

Also I don't imagine a 'label' by definition would have a 'value'. It would have text or HTML content, thus innerHTML is used to get or set its content. I have refactored your function this way:
function TotalUsedShares07() {
	var value1 = document.getElementById('Number1').value,
	value2 = document.getElementById('Number2').value,
	value3 = document.getElementById('Number3').value,
	theTotal = (parseFloat(value1) + parseFloat(value2) + parseFloat(value3));
	
	document.getElementById('ResultTotal').innerHTML = theTotal;
}

Open in new window

and tested with this code, and it works fine...
<div class="row">
	<input type="number" id="Number1" name="Number1">
	<input type="number" id="Number2" name="Number2">
	<input type="number" id="Number3" name="Number3">
	<label id="ResultTotal"></label>
</div>
<div class="row">
	<input type="button" value="Set" onClick="TotalUsedShares07()">
</div>

Open in new window

Avatar of fwstealer
fwstealer
Flag of United States of America image

ASKER

yes - so  on any of the textboxes used i do the following:

                <asp:TextBox ID="txtbx07d" runat="server" Width="50" onchange="TotalUsedShares07()"></asp:TextBox>
Avatar of fwstealer
fwstealer
Flag of United States of America image

ASKER

my error when i try:  

function TotalUsedShares07()
{
    var _txtBox1 = document.getElementById('<%= txtbx07d.ClientID %>').value;            
    var _txtBox2 = document.getElementById('<%= txtbx07e.ClientID %>').value;
    var _txtBox3 = document.getElementById('<%= txtbx07f.ClientID %>').value;

    var value1 = _txtBox1;            
    var value2 = _txtBox2;
    var value3 = _txtBox3;
    var valSum = parseFloat(value1) + parseFloat(value2) + parseFloat(value3)

    theTotal = valSum;
      alert(theTotal);
      document.getElementById('lbl07g').innerHTML = theTotal;

}

Microsoft JScript runtime error: Unable to set value of the property 'innerHTML': object is null or undefined
Avatar of fwstealer
fwstealer
Flag of United States of America image

ASKER

<asp:Label ID="lbl07g" runat="server"  ></asp:Label> is how the label is set up
Avatar of Ovid Burke
Ovid Burke
Flag of Saint Vincent and the Grenadines image

I think your IDs of your Textboxes might be the problem. Particularly the .ClientID portion. Double check those.
Hi,

ASP:Label will be rendered as SPAN tag in the web browser! So you have to use innerHTML for sure!

More over, ClientID is required!
document.getElementById("<%=lbl07g.ClientID%>").innerHTML = theTotal;

Open in new window


onchange event will be fired, when you tab out of the text box, not when you edit the text! instead you can try onkeydown or onkeypress events!

Hope it helps u...
ASKER CERTIFIED SOLUTION
Avatar of Ovid Burke
Ovid Burke
Flag of Saint Vincent and the Grenadines image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Knakworst
Knakworst

I would also set the ClientIDMode of the Label to Static.
<asp:Label ID="lblo7g" runat="server" ClientIDMode="Static"></asp:Label>

This way you dont have to use ClientID in javascript:
var label = document.getElementById("lbl07g");

You can also do this for the textboxes of course.
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo