homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Accessing Form Array Values

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

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...


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



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

What does your HTML look like?


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

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 = ...;


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

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>

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

Cheers, Pete


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


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

Didn't seem to work either.


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


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

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 = ...;

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved