SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    runat="server" Golgotha's Avatar
    Join Date
    Nov 2001
    Location
    Colorado
    Posts
    2,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get the selected values from a mutliple select box?

    <select multiple="true" size="4" name="multiAccounts" >
    <option value="5000">5000</option>
    <option value="6000">6000</option>
    <option value="6500">6500</option>
    <option value="7000">7000</option>
    </select>

    How can you get the selected values?

    this doesn't work?
    var getAccounts = document.getElementById("multiAccounts");
    alert(getAccounts.options.length);

  2. #2
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you checked your error console? getElementById() only accepts valid ID values, not name values.

    Code:
    <select multiple="true" size="4" name="multiAccounts" id="multiAccounts">
    <option value="5000">5000</option>
    <option value="6000">6000</option>
    <option value="6500">6500</option>
    <option value="7000">7000</option>
    </select>
    
    var selectMenu = document.getElementById('multiAccounts')
    for (var i=selectMenu.length; i--;) { if (selectMenu.options[i].selected == true) { arr.push(selectMenu.options[i]); } }; arr;
    I just did it in console and it seemed to work.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    You can though use getElementsByName, and take the first element from the returned array

    var getAccounts = document.getElementsByName("multiAccounts")[0];
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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