Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4

Thread: Select box help

  1. #1
    Senior Member
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    735
    Member #
    1935
    what is wrong with the following code?? I am trying to get the value of the currently selected element in a select box.
    Code:
     alert(document.addForm.product[document.addForm.product.selectedIndex].value);
    I have double checked, the form is named 'addForm' and the select box is named 'product'..

  2.  

  3. #2
    Senior Member skrlin's Avatar
    Join Date
    Apr 2003
    Location
    Illinois
    Posts
    562
    Member #
    1063
    I can't see any problems with that particular bit of code, what about the HTML for the form/select. How is that written ? A link to, or a posted chunk of the code would help. Are you getting any javascript errors ? If so, what are they ? Things like these are important to troubleshooting problems like this. You've left your question a bit too vague. Try to keep these things in mind when you post questions pertaining to javascript.

    EDIT: Also, is the script in the code before or after the body tag?

    For instance:
    Code:
    <html><head>
    <script>
    alert(document.addForm.product[document.addForm.product.selectedIndex].value);
    </script>
    </head>
    <body>
    <form name="addForm">
     <select name="product">
      <option value="0" selected>option 0</option>
      <option value="1">option 1</option>
      <option value="2">option 2</option>
     </select>
    </form>
    </body>
    </html>
    This example won't work as the script will run before the select is loaded. It's better to use this instead:
    Code:
    <html><head>
    <script>
    function alertVal() {
     alert(document.addForm.product[document.addForm.product.selectedIndex].value);
    }
    </script>
    </head>
    <body>
    <form name="addForm">
     <select name="product" onChange="alertVal()">
      <option value="0" selected>option 0</option>
      <option value="1">option 1</option>
      <option value="2">option 2</option>
     </select>
    </form>
    </body>
    </html>
    This way the select is already loaded and the script won't run untill the select box is acually changed.
    - Brian

  4. #3
    Senior Member
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    735
    Member #
    1935
    The code is all generated by an ASP script i have written for a client..

    This relevant output HTML is below.
    Code:
    <form action="admin_store_categories.asp" method="get" name="addForm">
      <select name="product">
         <option selected="selected" value="empty">Choose a Product</option> 
         <option value="32">Brenda</option>
         <option value="31">Mary-s</option>
      </select>
    </form>
    The JS function is called once the submit button is pressed. The JS is located in the <head> tag

  5. #4
    Senior Member skrlin's Avatar
    Join Date
    Apr 2003
    Location
    Illinois
    Posts
    562
    Member #
    1063
    I still can't see why this would cause problems.

    I've uploaded a working version of what you are trying to do...

    Are you getting any js errors ? If so what are they ?

    Is there any other chunk of code that may be causing a problem ?
    - Brian


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 08:30 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com