Link to home
Start Free TrialLog in
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);
}
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

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;
}
How do you call or trigger function TotalUsedShares07() ? Can you show me the HTML code?
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

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

                <asp:TextBox ID="txtbx07d" runat="server" Width="50" onchange="TotalUsedShares07()"></asp:TextBox>
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
<asp:Label ID="lbl07g" runat="server"  ></asp:Label> is how the label is set up
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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.