Javascript Help

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

LVL 6
nickinthoozAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
leakim971Connect With a Mentor PluritechnicianCommented:
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
 
Robert SchuttSoftware EngineerCommented:
possibly you need:
var numnights = document.getElementById('numberNights'+i).value;

Open in new window

(and for the others as well)
0
 
Meir RivkinFull stack Software EngineerCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
leakim971PluritechnicianCommented:
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
 
Robert SchuttSoftware EngineerCommented:
Indeed. But it would be nice to see some html to make sure this will work.
0
 
nickinthoozAuthor Commented:
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
 
Robert SchuttSoftware EngineerCommented:
maybe it's:
var numnights = document.getElementById('numberNights['+i+']').value; 

Open in new window

0
 
nickinthoozAuthor Commented:
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
 
leakim971PluritechnicianCommented:
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
 
Robert SchuttSoftware EngineerCommented:
Or use the form to access elements, something like:
var numnights = document.form['yourFormName'].elements['numberNights'+i].value;

Open in new window

0
 
leakim971PluritechnicianCommented:
rigth click on your page in your web browser, choose view source and post it here
thanks
0
 
nickinthoozAuthor Commented:
I think it may be the id's, I think I may be on crack...
0
 
Robert SchuttSoftware EngineerCommented:
;-)

Some inputs have an id, but not all. Either correct that or try my suggestion regarding document.form
0
 
nickinthoozAuthor Commented:
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
 
Robert SchuttSoftware EngineerCommented:
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
 
Robert SchuttSoftware EngineerCommented:
What does your form tag look like?
0
 
Robert SchuttConnect With a Mentor Software EngineerCommented:
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
 
nickinthoozAuthor Commented:
<?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
 
Robert SchuttConnect With a Mentor Software EngineerCommented:
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
 
Robert SchuttSoftware EngineerCommented:
@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
 
Robert SchuttSoftware EngineerCommented:
Nick, I see you posted your form tag but in the mean time I found it wasn't necessary.
0
 
nickinthoozAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.