#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    65
    Rep Power
    15
    I have a form with three select fields. Each of the options has a value, which is a price. How do I have javascript read the values of the three selected options? I want it to read them and total the prices.
    TIA,
    Ben
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    81
    Rep Power
    15
    price1 = form.elements[0].options[0].value
    price2 = form.elements[0].options[1].value
    price3 = form.elements[0].options[2].value
    .
    .
    price5 = form.elements[1].options[1].value
    .
    .
    price9 = form.elements[2].options[2].value

    You will need to adjust the array index of elements to match your form. The index is assigned by how the elements appear in the code, not on the form. Or you can replace elements[x] with the name of the element if you gave it one. Each of the three select fields will have however many options you assign accessed by options[x] starting with options[0].

    Russ

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by FerretComp:
    I have a form with three select fields. Each of the options has a value, which is a price. How do I have javascript read the values of the three selected options? I want it to read them and total the prices.
    TIA,
    Ben
    [/quote]

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    65
    Rep Power
    15
    Is there a way to get the value for the option the user has selected?
    Ben
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Posts
    84
    Rep Power
    15
    To get the value that the user selects from the selectbox you can do it like this:

    price = document.formname.selectboxname.options[document.formname.selectboxname.selectedIndex].value;

    Spookster

    ------------------
    Visit the WSAbstract Help Forum for your Javascript, DHTML, or General Web Design Questions
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    65
    Rep Power
    15
    Thanks, that really cleans up my code a lot.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Posts
    84
    Rep Power
    15
    You can even shorten that down by using a variable:

    theForm = document.formname.selectboxname
    price = theForm.options[theForm.selectedIndex].value;

    Spookster

    ------------------
    Visit the WSAbstract Help Forum for your Javascript, DHTML, or General Web Design Questions

Similar Threads

  1. Replies: 3
    Last Post: October 8th, 2003, 01:07 PM
  2. not possible to SELECT-WHERE primary key fields?
    By mithril in forum MySQL Help
    Replies: 2
    Last Post: July 18th, 2003, 02:16 PM
  3. MySQL vs PostgreSQL
    By Adman in forum Database Management
    Replies: 46
    Last Post: September 10th, 2002, 12:48 AM
  4. Replies: 4
    Last Post: February 18th, 2002, 06:08 AM
  5. How do I shorten marking select boxes "selected"
    By Little John in forum Perl Programming
    Replies: 10
    Last Post: December 27th, 2001, 06:23 PM

IMN logo majestic logo threadwatch logo seochat tools logo