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
    Aug 2006
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Get the value from a select form

    Dear All

    I want to get the value from a select option in a form. It works in FF but not in IE7 which just returns a blank alert window.

    I suspect I am doing something pretty stupid!

    Any ideas please!

    Here's the html

    Code:
    <script  src="pafForm.js" type="text/javascript"></script>
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="contact.html" name="pafEnquiries" method="post">
    <select id="projectType">
       <option>Please select</option>
       <option>Commercial</option>
       <option>Residentail</option>
       <option>Industrial</option>
       </select> 
    </form>
    and here's the js

    Code:
    window.onload = initForm;
     
    function initForm(){
     document.getElementById("projectType").selectedIndex=0;
     document.getElementById("projectType").onchange=populateApplication;
    }
    function populateApplication(){
     
     var edNum = document.getElementById("projectType").selectedIndex;
     var edValue = document.getElementById("projectType").options[edNum].value;
     alert (edValue);
    }

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by maxelcat View Post
    Code:
    <select id="projectType">
       <option>Please select</option>
       <option>Commercial</option>
       <option>Residentail</option>
       <option>Industrial</option>
       </select> 
    </form>
    did you try setting the options value attribute

    Code:
    <select id="projectType">
       <option value="Please select">Please select</option>
       <option value="Commercial">Commercial</option>
       <option value="Residentail">Residentail</option>
       <option value="Industrial">Industrial</option>
       </select> 
    </form>
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,299
    Thanks
    10
    Thanked 585 Times in 566 Posts
    no need to recode, just fix your bug:

    var edValue = document.getElementById("projectType").options[edNum].value;

    should be:
    var edValue = document.getElementById("projectType").options[edNum].text;
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #4
    New Coder
    Join Date
    Aug 2006
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks to both of you.

    I sorted it by adding the value attribute, but I could see how the .text fix would work too!


  •  

    Posting Permissions

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