...

View Full Version : Referencing values on a drop-down menu



trevorwilson
05-08-2006, 07:26 PM
I apologise for my ignorance in adavnce! Trying very hard to learn a little Javascript for my site and have a question relating to form calculations. I have managed to write a working script to add together two values and display the total. I need to advance from that stage and now I have a 'quantity' box and a drop-down menu with a few options on it, each relating to a price. I am trying to take the quantity entered by the user and multiply it by the price they select from the drop-down and display it in a third box. So far I have:

one = document.autoSumForm.quant1.value;
two = document.autoSumForm.value from menu.value;
document.autoSumForm.total.value = (one * 1) + (two * 1);

How can I rewrite the above to account for the different choices a user can make on a drop-down menu?

Beagle
05-08-2006, 07:28 PM
javascript objects can be accessed using both dot notation and array notation, so if you have a variable that represents a property name you can do:

var field = "inputFieldName";
var val = document.form[field].value;

Thompson
05-08-2006, 07:45 PM
Remembering that if you have only 1 element in you form, and treat it like an array, it will cause a javascript error. you can dodge this error verifying the length of the object.

if( obj.length == "undefined" )
object = obj.value;
else
object = obj[0].value;

for example.

hope this helped you :thumbsup:

trevorwilson
05-08-2006, 08:41 PM
javascript objects can be accessed using both dot notation and array notation, so if you have a variable that represents a property name you can do:

var field = "inputFieldName";
var val = document.form[field].value;

Thanks Beagle - I am afraid I am just not getting it though (I am not really very good at programming!). I understand what a variable is in general but I can't work out how to relate it to my scenario.

My drop down menu has, for example, two items as such:

<option value="5x7">5x7 - &pound;7.00</option>
<option value="8x6">8x6 - &pound;11.00</option>


If I want option 5x7 to have a value of 7 and 8x6 to have a value of 11 for my calculations how would I include this in my script?

Following on from this, I intend to have more lines on my form for extra items. Obviously I can reference the first box easily enough (quant1, quant2, quant 3 etc.) but how would this work for the drop-down menus? (The values will stay the same for each item though.)

Thanks for your patience!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum