Welcome to WebmasterWorld Guest from 54.196.224.166

Forum Moderators: open

Message Too Old, No Replies

Accessing Form Array Values

     

PeteM

7:05 pm on Jan 17, 2009 (gmt 0)

10+ Year Member



I'm using the following statement to access some form variables that are held in an array but I am getting an 'is null or not an object' error...

document.orderDetails.ItemTotal[i].value

Here's the statement in context...

var allItemCost = document.getElementsByName("ItemCost[]");

for (var i=0; i < allItemCost.length; i++)
{
document.orderDetails.ItemTotal[i].value =
parseFloat(document.orderDetails.ItemCost[i].value) *
parseFloat(document.orderDetails.ItemQuantity[i].value) *
( 1 - ( parseFloat(document.orderDetails.ItemDiscount[i].value)/100 ) );
}

Any ideas?

Cheers, Pete

Fotiman

4:03 am on Jan 18, 2009 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



What does your HTML look like?

Fotiman

4:31 am on Jan 18, 2009 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



var allItemCost = document.getElementsByName("ItemCost[]");

That statement is going to find elements that have a name value of "ItemCost[]". In other words, it will not match elements with names like "ItemCost[1]" or "ItemCost[2]".

The statement:
document.orderDetails.ItemTotal[i].value = ...;

will not work as you'd like. Try this instead:

document.orderDetails['ItemTotal[' + i + ']'].value = ...;

PeteM

10:08 am on Jan 18, 2009 (gmt 0)

10+ Year Member



Thanks for your suggestions.

HTML looks like this....

<form name="orderDetails" action="protxautoform.php" method="POST">
...
<input type="hidden" name="ItemName[]" value="Some stuff">
<input type="hidden" name="ItemCost[]" value="8.00">
<input type="hidden" name="ItemQuantity[]" value="1">
<input type="hidden" name="ItemSubTotal[]" value="8.00">
<input type="text" name="ItemDiscount[]" value="0" onFocus="startCalc();" onBlur="stopCalc();">
<input type="text" name="ItemTotal[]" size="5" readonly>
...
</form>

The input fields above repeat (they are in a table of cart items).

Cheers, Pete

PeteM

10:50 am on Jan 18, 2009 (gmt 0)

10+ Year Member



Fotiman,

document.orderDetails['ItemTotal[' + i + ']'].value = ...;

Didn't seem to work either.

Pete

[edited by: PeteM at 10:51 am (utc) on Jan. 18, 2009]

Fotiman

5:06 pm on Jan 18, 2009 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Ah, well based on your HTML it seems like you will have several text boxes all named "ItemTotal[]". They are not named "ItemTotal[1]", etc. I'm not exactly sure what you're trying to do. Are you trying to update all of the fields named "ItemTotal[]" or only 1 field named "ItemTotal[]"? If you're trying to do this in sync with the ItemCost[] elements, then you would need to do something like:

var allItemTotal = document.getElementsByName('ItemTotal[]');
allItemTotal[i].value = ...;

 

Featured Threads

Hot Threads This Week

Hot Threads This Month