Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-18-2012, 03:40 PM   PM User | #1
tburger
New to the CF scene

 
Join Date: Jan 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
tburger is an unknown quantity at this point
Help with calculating the total of fields on a web page

Folks,

I am trying to calculate the total of fields on a web form.
I have multiple rows of 10 fields with a total for each.
Each row comes from a different record in a database.
I am only showing 2 fields of the 10 for simplicity.

The name and id of each field looks like
"Q300_1" onchange="update(Q300)"
"Q300_2" onchange="update(Q300)"
"Q300_t"
"Q301_1" onchange="update(Q301)"
"Q301_2" onchange="update(Q301)"
"Q301_t"


My current attempt at the script looks like:
[CODE]
<script type="text/javascript">
function update(item) {
var itemt=item + "_t";
var item1=item + "_1";
var item2=item + "_2";
document.myform[itemt].value = (document.myform[item1].value -0) + (document.myform[item2].value -0);
}
</script>
[CODE]

It does not work and I get
Error: Q301 is not defined
in the Firefox error console.
tburger is offline   Reply With Quote
Old 01-18-2012, 03:45 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
I try it with a simple hint first

Code:
update(Q301)
Here Q301 will be interpreted as a variable identifier whose content will be the first paramter to update()

Code:
update('Q301')
Here 'Q301' is a literal string containing 4 characters. This string will be the first parameter to update()

What do you think?
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
tburger (01-18-2012)
Old 01-18-2012, 04:20 PM   PM User | #3
tburger
New to the CF scene

 
Join Date: Jan 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
tburger is an unknown quantity at this point
That fixed it.

Thank you very much.

Maybe I can find all my hair on the floor and glue it back on my head! ;-)
tburger is offline   Reply With Quote
Old 01-18-2012, 05:57 PM   PM User | #4
tburger
New to the CF scene

 
Join Date: Jan 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
tburger is an unknown quantity at this point
One more question.
As I said early I have 10 fields to add.
But sometimes I only have 8 fields on the form.
How do I test to make sure a field is there so the script does not generate an 'undefined' error?
tburger is offline   Reply With Quote
Old 01-18-2012, 06:33 PM   PM User | #5
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Code:
// check if specific form element exists
if(document.myform[itemX]) {
   ....
}
devnull69 is offline   Reply With Quote
Old 01-18-2012, 07:15 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by tburger View Post
One more question.
As I said early I have 10 fields to add.
But sometimes I only have 8 fields on the form.
How do I test to make sure a field is there so the script does not generate an 'undefined' error?
Like this:-
Code:
<form name= "myform">
<input type = "text" name = "Amount1">
<input type = "text" name = "Amount2">
<input type = "text" name = "Amount3">
<!-- field Amount 4 does not exist -->
<input type = "button" value = "Get Total" onclick = "doTotal(this.form)">
<input type = "text" name = "Total">
</form>

<script type = "text/javascript">

function doTotal(f) { 

var a=b=c=d=0;  // initialise all to 0

if (f.Amount1 != undefined) { a = Number(f.Amount1.value) || 0; f.Amount1.value = a}
if (f.Amount2 != undefined) { b = Number(f.Amount2.value) || 0; f.Amount2.value = b}
if (f.Amount3 != undefined) { c = Number(f.Amount3.value) || 0; f.Amount3.value = c}
if (f.Amount4 != undefined) { d = Number(f.Amount4.value) || 0; f.Amount4.value = d}

f.Total.value = a + b + c + d; 
}
 
</script>
If you want to use devnull's square bracket notation you can do

Code:
if (f["Amount4"]) { d = Number(f.Amount4.value) || 0; f.Amount4.value = d}  // note that the field name ["Amount4"] is in quotes
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 01-18-2012 at 08:00 PM.. Reason: Typo
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:52 AM.


Advertisement
Log in to turn off these ads.