By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,435 Members | 2,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,435 IT Pros & Developers. It's quick & easy.

Calculate row total with javascript

P: n/a
Quick question. I have some java script that looks like this;

imgObj.parentNode.parentNode.childNodes(10).childN odes(0).value=imgObj.parentNode.parentNode.childNo des(6).innerText-imgObj.parentNode.parentNode.childNodes(9).childNo des(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!

Nov 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
suggestion:

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

"Lyners" <Ly****@discussions.microsoft.com> wrote in message
news:EE**********************************@microsof t.com...
Quick question. I have some java script that looks like this;

imgObj.parentNode.parentNode.childNodes(10).childN odes(0).value=imgObj.parentNode.parentNode.childNo des(6).innerText-imgObj.parentNode.parentNode.childNodes(9).childNo des(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!

Nov 19 '05 #2

P: n/a
Hi Lyners,

use the parseInt or parseFloat:

parseInt("abc") // Returns NaN.
parseInt("12abc") // Returns 12.
parseFloat("abc") // Returns NaN.
parseFloat("1.2abc") // Returns 1.2.

HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Lyners" wrote:
Quick question. I have some java script that looks like this;

imgObj.parentNode.parentNode.childNodes(10).childN odes(0).value=imgObj.parentNode.parentNode.childNo des(6).innerText-imgObj.parentNode.parentNode.childNodes(9).childNo des(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!

Nov 19 '05 #3

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

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

Any suggestions?

Thanks

"Phillip Williams" wrote:
Hi Lyners,

use the parseInt or parseFloat:

parseInt("abc") // Returns NaN.
parseInt("12abc") // Returns 12.
parseFloat("abc") // Returns NaN.
parseFloat("1.2abc") // Returns 1.2.

HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Lyners" wrote:
Quick question. I have some java script that looks like this;

imgObj.parentNode.parentNode.childNodes(10).childN odes(0).value=imgObj.parentNode.parentNode.childNo des(6).innerText-imgObj.parentNode.parentNode.childNodes(9).childNo des(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!

Nov 19 '05 #4

P: n/a
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!

"Grant Merwitz" wrote:
suggestion:

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

"Lyners" <Ly****@discussions.microsoft.com> wrote in message
news:EE**********************************@microsof t.com...
Quick question. I have some java script that looks like this;

imgObj.parentNode.parentNode.childNodes(10).childN odes(0).value=imgObj.parentNode.parentNode.childNo des(6).innerText-imgObj.parentNode.parentNode.childNodes(9).childNo des(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!


Nov 19 '05 #5

P: n/a
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).innerT ext + "= " +
parseFloat(imgObj.parentNode.parentNode.childNodes (6).innerText));

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

Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Lyners" wrote:
Hi Phillip,
I tried the parseFloat, but got the same results. Here's my javascript:

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

Any suggestions?

Thanks

"Phillip Williams" wrote:
Hi Lyners,

use the parseInt or parseFloat:

parseInt("abc") // Returns NaN.
parseInt("12abc") // Returns 12.
parseFloat("abc") // Returns NaN.
parseFloat("1.2abc") // Returns 1.2.

HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Lyners" wrote:
Quick question. I have some java script that looks like this;

imgObj.parentNode.parentNode.childNodes(10).childN odes(0).value=imgObj.parentNode.parentNode.childNo des(6).innerText-imgObj.parentNode.parentNode.childNodes(9).childNo des(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!

Nov 19 '05 #6

P: n/a
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).childN odes(0).value=parseFloat(imgObj.parentNode.parentN ode.childNodes(6).innerText)-parseFloat(imgObj.parentNode.parentNode.childNodes (9).childNodes(1).value);

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

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

Any suggestions?

Thank you!

"Phillip Williams" wrote:
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).innerT ext + "= " +
parseFloat(imgObj.parentNode.parentNode.childNodes (6).innerText));

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

Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Lyners" wrote:
Hi Phillip,
I tried the parseFloat, but got the same results. Here's my javascript:

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

Any suggestions?

Thanks

"Phillip Williams" wrote:
Hi Lyners,

use the parseInt or parseFloat:

parseInt("abc") // Returns NaN.
parseInt("12abc") // Returns 12.
parseFloat("abc") // Returns NaN.
parseFloat("1.2abc") // Returns 1.2.

HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Lyners" wrote:

> Quick question. I have some java script that looks like this;
>
> imgObj.parentNode.parentNode.childNodes(10).childN odes(0).value=imgObj.parentNode.parentNode.childNo des(6).innerText-imgObj.parentNode.parentNode.childNodes(9).childNo des(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!
>

Nov 19 '05 #7

P: n/a
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?

"Lyners" wrote:
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).childN odes(0).value=parseFloat(imgObj.parentNode.parentN ode.childNodes(6).innerText)-parseFloat(imgObj.parentNode.parentNode.childNodes (9).childNodes(1).value);

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

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

Any suggestions?

Thank you!

"Phillip Williams" wrote:
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).innerT ext + "= " +
parseFloat(imgObj.parentNode.parentNode.childNodes (6).innerText));

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

Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Lyners" wrote:
Hi Phillip,
I tried the parseFloat, but got the same results. Here's my javascript:

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

Any suggestions?

Thanks

"Phillip Williams" wrote:

> Hi Lyners,
>
> use the parseInt or parseFloat:
>
> parseInt("abc") // Returns NaN.
> parseInt("12abc") // Returns 12.
> parseFloat("abc") // Returns NaN.
> parseFloat("1.2abc") // Returns 1.2.
>
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Lyners" wrote:
>
> > Quick question. I have some java script that looks like this;
> >
> > imgObj.parentNode.parentNode.childNodes(10).childN odes(0).value=imgObj.parentNode.parentNode.childNo des(6).innerText-imgObj.parentNode.parentNode.childNodes(9).childNo des(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!
> >

Nov 19 '05 #8

P: n/a
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

"Lyners" wrote:
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).childN odes(0).value=parseFloat(imgObj.parentNode.parentN ode.childNodes(6).innerText)-parseFloat(imgObj.parentNode.parentNode.childNodes (9).childNodes(1).value);

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

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

Any suggestions?

Thank you!

"Phillip Williams" wrote:
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).innerT ext + "= " +
parseFloat(imgObj.parentNode.parentNode.childNodes (6).innerText));

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

Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Lyners" wrote:
Hi Phillip,
I tried the parseFloat, but got the same results. Here's my javascript:

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

Any suggestions?

Thanks

"Phillip Williams" wrote:

> Hi Lyners,
>
> use the parseInt or parseFloat:
>
> parseInt("abc") // Returns NaN.
> parseInt("12abc") // Returns 12.
> parseFloat("abc") // Returns NaN.
> parseFloat("1.2abc") // Returns 1.2.
>
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Lyners" wrote:
>
> > Quick question. I have some java script that looks like this;
> >
> > imgObj.parentNode.parentNode.childNodes(10).childN odes(0).value=imgObj.parentNode.parentNode.childNo des(6).innerText-imgObj.parentNode.parentNode.childNodes(9).childNo des(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!
> >

Nov 19 '05 #9

P: n/a
Phillip, Thanks again, you've done it.

Here is my final code;

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

Thanks again....
Lyners

"Phillip Williams" wrote:
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

"Lyners" wrote:
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).childN odes(0).value=parseFloat(imgObj.parentNode.parentN ode.childNodes(6).innerText)-parseFloat(imgObj.parentNode.parentNode.childNodes (9).childNodes(1).value);

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

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

Any suggestions?

Thank you!

"Phillip Williams" wrote:
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).innerT ext + "= " +
parseFloat(imgObj.parentNode.parentNode.childNodes (6).innerText));

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

Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Lyners" wrote:

> Hi Phillip,
> I tried the parseFloat, but got the same results. Here's my javascript:
>
> imgObj.parentNode.parentNode.childNodes(10).childN odes(0).value=parseFloat(imgObj.parentNode.parentN ode.childNodes(6).innerText)-parseFloat(imgObj.parentNode.parentNode.childNodes (9).childNodes(0).value)
> I'll try the parseInt.
>
> Any suggestions?
>
> Thanks
>
> "Phillip Williams" wrote:
>
> > Hi Lyners,
> >
> > use the parseInt or parseFloat:
> >
> > parseInt("abc") // Returns NaN.
> > parseInt("12abc") // Returns 12.
> > parseFloat("abc") // Returns NaN.
> > parseFloat("1.2abc") // Returns 1.2.
> >
> > HTH,
> > Phillip Williams
> > http://www.societopia.net
> > http://www.webswapp.com
> >
> >
> > "Lyners" wrote:
> >
> > > Quick question. I have some java script that looks like this;
> > >
> > > imgObj.parentNode.parentNode.childNodes(10).childN odes(0).value=imgObj.parentNode.parentNode.childNo des(6).innerText-imgObj.parentNode.parentNode.childNodes(9).childNo des(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!
> > >

Nov 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.