Calculate row total with javascript

G

Guest

Quick question. I have some java script that looks like this;

imgObj.parentNode.parentNode.childNodes(10).childNodes(0).value=imgObj.parentNode.parentNode.childNodes(6).innerText-imgObj.parentNode.parentNode.childNodes(9).childNodes(0).value

This takes a cell in a datagrid (innertext) and subtracts the value of a
text box (value) in another cell, then populates a second text box in a third
cell with the value. it works except for the value it is populating in the
third cell is "NaN", which I think is "Not a Numeric". I tried using the
val() clause to make the values numbers, but this did not work. How do I make
the text values numbers in javascript?

Thanks!
 
G

Grant Merwitz

suggestion:

Why not work it out through the Code Behind, and populate a hidden on the
page that the JavaScript can pick up
 
G

Guest

Hi Phillip,
I tried the parseFloat, but got the same results. Here's my javascript:

imgObj.parentNode.parentNode.childNodes(10).childNodes(0).value=parseFloat(imgObj.parentNode.parentNode.childNodes(6).innerText)-parseFloat(imgObj.parentNode.parentNode.childNodes(9).childNodes(0).value)
I'll try the parseInt.

Any suggestions?

Thanks
 
G

Guest

Hi Grant, when you say Code Behind, do you mean do the calculations on the
Post Back? I am trying to avaid doing a Post Back, What I want to do is have
the totals update on the fly while the user is editing the datagrid. There
are about 100 entries on the screen. If I did a Post back on everyone of
them, it would consume a lot of time.

Thanks!
 
G

Guest

Hi Lyners,

You might want to do a diagnostic alert on each statement to find out if you
are picking the right node or not, e.g.
alert (imgObj.parentNode.parentNode.childNodes(6).innerText + "= " +
parseFloat(imgObj.parentNode.parentNode.childNodes(6).innerText));

alert(parseFloat(imgObj.parentNode.parentNode.childNodes(9).childNodes(0).value+
"=" +
parseFloat(imgObj.parentNode.parentNode.childNodes(9).childNodes(0).value));

Phillip Williams
http://www.societopia.net
http://www.webswapp.com
 
G

Guest

Hi Phillip,
I should have remembered from your previous post to use the all important
alert when troubleshooting. What I have determined is that the parser doesn't
like the $ sign in the innertext field. If I have a number like 222.00 it
works, If I have $222.00 it doesn't work. I did find out I was referenceing
the wrong control, but I still have the NaN problems because of the "$"
problem.

Here is my updated code;

imgObj.parentNode.parentNode.childNodes(10).childNodes(0).value=parseFloat(imgObj.parentNode.parentNode.childNodes(6).innerText)-parseFloat(imgObj.parentNode.parentNode.childNodes(9).childNodes(1).value);

alert(imgObj.parentNode.parentNode.childNodes(6).innerText + "= " +
parseFloat(imgObj.parentNode.parentNode.childNodes(6).innerText));

alert(imgObj.parentNode.parentNode.childNodes(9).childNodes(1).value+ "=" +
parseFloat(imgObj.parentNode.parentNode.childNodes(9).childNodes(1).value));

Any suggestions?

Thank you!
 
G

Guest

I should add.... I guess what I am trying to do is add a field formated as
currency with another field. I wonder if I should have a hidden field with
the value minus the formating to add the columns together?
 
G

Guest

Hi Lyners,

To remove unwanted formatting characters from a string, I would use regular
expressions with the replace function, e.g. if you have $1,234,567 and wants
to get 1234567:

var textVal = " $ 1,234,567";
var numericVal=0;
var re=/\$+/g; //this is a regular expression to remove the $ sign
textVal = textVal.replace(re, ""); //this executes the regular expression
alert(textVal);

re=/(,)+/g; //this regular expression removes the commas
textVal = textVal.replace(re, "");
alert(textVal);

HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
 
G

Guest

Phillip, Thanks again, you've done it.

Here is my final code;

var textval = imgObj.parentNode.parentNode.childNodes(6).innerText;
var re=/\$+/g;
textval = textval.replace(re,"");
re =/(,)+/g;
textval = textval.replace(re,"");
var textval2 =
imgObj.parentNode.parentNode.childNodes(9).childNodes(1).value;
var re=/\$+/g;
textval2 = textval2.replace(re,"");
re =/(,)+/g;
textval2 = textval2.replace(re,"")
imgObj.parentNode.parentNode.childNodes(10).childNodes(0).value=parseFloat(textval)-parseFloat(textval2);

Thanks again....
Lyners
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top