Solved

Javascript Help

Posted on 2013-01-28
22
250 Views
Last Modified: 2013-01-28
Hey,

I'm trying to loop through fields that have number appended to them in php and calculate a running total in each total field.  This is the code I have so far:

function calculate(form)
{
	for (var i=1; i<6; i++)
	{
var numnights = document.getElementById(numberNights[i]).value;
var rate = document.getElementById(rate[i]).value
var tax = document.getElementById(taxAmount[i]).value
var total = document.getElementById(totalCost[i]).value
var subtotal = parseFloat(document.getElementById(rate[i]).value) + parseFloat(document.getElementById(taxAmount[i]).value) ;
var total = subtotal * parseFloat(document.getElementById(numberNights[i]).value);
document.getElementById(totalCost[i]).value(total.toFixed(2));
	}
}

I keep getting "numberNights[i] is not defined",  I admittedly suck at javascript.

Open in new window

0
Comment
Question by:nickinthooz
  • 11
  • 6
  • 4
  • +1
22 Comments
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
possibly you need:
var numnights = document.getElementById('numberNights'+i).value;

Open in new window

(and for the others as well)
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
i guess u meant that:

function calculate(form)
{
	for (var i=1; i<6; i++)
	{
var numnights = document.getElementById('numberNights'+i).value;
var rate = document.getElementById('rate'+i).value
var tax = document.getElementById('taxAmount'+i).value
var total = document.getElementById('totalCost'+i).value
var subtotal = parseFloat(document.getElementById('rate'+i).value) + parseFloat(document.getElementById('taxAmount'+i).value) ;
var total = subtotal * parseFloat(document.getElementById('numberNights'+i).value);
document.getElementById('totalCost'+i).value(total.toFixed(2));
	}
}

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
try this :
function calculate(form)
{
	for (var i=1; i<6; i++)
	{
var numnights = document.getElementById("numberNights"+i).value;
var rate = document.getElementById(rate+i).value
var tax = document.getElementById(taxAmount+i).value
var total = document.getElementById(totalCost+i).value
var subtotal = parseFloat(document.getElementById(rate+i).value) + parseFloat(document.getElementById(taxAmount+i).value) ;
var total = subtotal * parseFloat(document.getElementById(numberNights+i).value);
document.getElementById(totalCost+i).value =total.toFixed(2);
	}
}

Open in new window

0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
Indeed. But it would be nice to see some html to make sure this will work.
0
 
LVL 6

Author Comment

by:nickinthooz
Comment Utility
Thanks for the quick responses,  I'm still catching this error:

TypeError: document.getElementById(...) is null
[Break On This Error] 	

var numnights = document.getElementById("numberNights"+i).value;

Open in new window

0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
maybe it's:
var numnights = document.getElementById('numberNights['+i+']').value; 

Open in new window

0
 
LVL 6

Author Comment

by:nickinthooz
Comment Utility
as requested:

		<tr id="tableRow<?php echo $cnt; ?>" class="tableRow<?php echo $cnt; ?>">
										<td>  <button type="button" id="deleteButton<?php echo $cnt; ?>" name="deleteButton<?php echo $cnt; ?>" class="btn pull-right" <?php echo $disabled; ?> onclick="deleteRow(this);" /><li class="icon-trash"></li></button></td>
										<td><input type="text" name="roomNumber<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] span1" <?php echo $read_only; ?> value="<?php echo $stays->roomNumber; ?>"></td>
										<td><input type="text" name="name<?php echo $cnt; ?>" id="name<?php echo $cnt; ?>" class="validate[required] text-input span2" <?php echo $read_only; ?>   value="<?php echo $stays->name; ?>"></td>
										<td><input type="text" onblur="check_id(this.value, <?php echo $cnt; ?>);" name="employeeId<?php echo $cnt; ?>" id="employeeId<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] span1" <?php echo $read_only; ?>   value="<?php echo $stays->employeeId; ?>"></td>
										<td><input type="text" name="costCenter<?php echo $cnt; ?>" id="costCenter<?php echo $cnt; ?>" class="validate[required] text-input span1" <?php echo $read_only; ?>   value="<?php echo $stays->costCenter; ?>"></td>						
										<td><input type="text" style="width:45px;" class="validate[condRequired[name<?php echo $cnt; ?>]] span1" name="afeNumber<?php echo $cnt; ?>"  <?php echo $read_only; ?>   value="<?php echo $stays->afeNumber; ?>"></td>						
										<td><input type="text" name="dateIn<?php echo $cnt; ?>" id="dateIn<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] spanDate" <?php echo $read_only; ?>   value="<?php echo $dateIn; ?>"></td>
										<td><input type="text" name="dateOut<?php echo $cnt; ?>"  id="dateOut<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] spanDate" <?php echo $read_only; ?>   value="<?php echo $dateOut; ?>"></td>
										<td><input type="text" onchange="calculate(this)" name="numberNights<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] span1" <?php echo $read_only; ?>   value="<?php echo $stays->numberNights; ?>"></td>
										<td><input type="text" name="folioNumber<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] span1"  <?php echo $read_only; ?> value="<?php echo $stays->folioNumber; ?>"></td>
										<td><input type="text" onchange="calculate(this.form)" name="rate<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] span1" <?php echo $read_only; ?>  value="<?php echo $stays->rate; ?>"></td>
										<td><input type="text" onchange="calculate(this.form)" name="taxAmount<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] span1" <?php echo $read_only; ?>  value="<?php echo $stays->taxAmount; ?>"></td>
										<td><input type="text" name="totalCost<?php echo $cnt; ?>" class="validate[condRequired[name<?php echo $cnt; ?>]] span1" <?php echo $read_only; ?>   value="<?php echo $stays->totalCost; ?>"></td>
									  
									</tr>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
getElementById => get Element By Id

do you have something like this in your page :
<input type="text" name="THIS_IS_A_NAME" id="numberNights1" />
<input type="text" name="THIS_IS_A_NAME" id="numberNights2" />
<input type="text" name="THIS_IS_A_NAME" id="numberNights3" />
<input type="text" name="THIS_IS_A_NAME" id="numberNights4" />
<input type="text" name="THIS_IS_A_NAME" id="numberNights5" />
<input type="text" name="THIS_IS_A_NAME" id="numberNights6" />
0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
Or use the form to access elements, something like:
var numnights = document.form['yourFormName'].elements['numberNights'+i].value;

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
rigth click on your page in your web browser, choose view source and post it here
thanks
0
 
LVL 6

Author Comment

by:nickinthooz
Comment Utility
I think it may be the id's, I think I may be on crack...
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
;-)

Some inputs have an id, but not all. Either correct that or try my suggestion regarding document.form
0
 
LVL 6

Author Comment

by:nickinthooz
Comment Utility
That fixed the errors, now I'm getting no errors, but I'm not getting any results either.  Updated Code:

function calculate(form)
{
	for (var i=1; i<6; i++)
	{
var numnights = document.getElementsByName('numberNights'+i).value;
var rate = document.getElementsByName('rate'+i).value
var tax = document.getElementsByName('taxAmount'+i).value
var total = document.getElementsByName('totalCost'+i).value
var subtotal = parseFloat(document.getElementsByName('rate'+i).value) + parseFloat(document.getElementsByName('taxAmount'+i).value) ;
var total = subtotal * parseFloat(document.getElementsByName('numberNights'+i).value);
document.getElementsByName('totalCost'+i).value = (total.toFixed(2));
	}
}

Open in new window

0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
That's not what I suggested. It probably doesn't work because GetElementsByName returns an array of elements. Maybe you can fix it by taking the first element with [0] and then take the value but it's not a very nice solution.
0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
What does your form tag look like?
0
 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 150 total points
Comment Utility
scratch that question, it's in the function argument. try this:
function calculate(form)
{
	for (var i=1; i<6; i++)
	{
var numnights = form.elements['numberNights'+i].value;
var rate = form.elements['rate'+i].value;
var tax = form.elements['taxAmount'+i].value;
var total = form.elements['totalCost'+i].value;
var subtotal = parseFloat(rate) + parseFloat(tax);
var total = subtotal * parseFloat(numnights);
form.elements['totalCost'+i].value =total.toFixed(2);
	}
}

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 350 total points
Comment Utility
getElementsByName => get Elements by name return an array because you can have more than one element having the same name !

function calculate(form)
{
	for (var i=1; i<6; i++)
	{
var numnights = document.getElementsByName('numberNights')[i].value;
var rate = document.getElementsByName('rate')[i].value
var tax = document.getElementsByName('taxAmount')[i].value
var total = document.getElementsByName('totalCost')[i].value
var subtotal = parseFloat(document.getElementsByName('rate')[i].value) + parseFloat(document.getElementsByName('taxAmount')[i].value) ;
var total = subtotal * parseFloat(document.getElementsByName('numberNights')[i].value);
document.getElementsByName('totalCost')[i].value = (total.toFixed(2));
	}
}

Open in new window


...but was simpler to add an ID attribute to ALL your fields..
0
 
LVL 6

Author Comment

by:nickinthooz
Comment Utility
<?php 
				$attributes = array( 'id' => 'myform');

				echo form_open_multipart('voucher/update_voucher', $attributes);
				?>

Open in new window


produces <form action="http://vouchers.........net/index.php/voucher/update_voucher" method="post" accept-charset="utf-8" id="myform" enctype="multipart/form-data">
0
 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 150 total points
Comment Utility
one problem left in the html:
<input type="text" onchange="calculate(this)" name="numberNights

Open in new window

should be:
<input type="text" onchange="calculate(this.form)" name="numberNights

Open in new window

The others are already correct.
0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
@leakim971: yep, that's another way to solve it but the php code needs to be amended in that case as well because at the moment the counter is in the element name.
0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
Nick, I see you posted your form tag but in the mean time I found it wasn't necessary.
0
 
LVL 6

Author Comment

by:nickinthooz
Comment Utility
I went through and just added Id's to the fields I needed to use and it worked perfectly.  Thanks guys. Oversight on my part.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to count occurrences of each item in an array.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now