Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2006
    Location
    Near Glasgow
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Referencing values on a drop-down menu

    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?

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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;

  • #3
    New Coder
    Join Date
    Mar 2006
    Location
    São Paulo, Brazil
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    ----
    As coisas mais simples são as mais importantes.
    ---------
    Thompson M1A1: D-Day
    ----------------

  • #4
    New Coder
    Join Date
    Mar 2006
    Location
    Near Glasgow
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Beagle
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •