SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face Get values of multiselect box using jquery

    I am using multi-select dropdown and trying to fetch the values in jquery.

    Please help.

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2007
    Location
    North Yorkshire, UK
    Posts
    483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the jquery documentation at .val() – jQuery API demonstrates it nicely. Essentially

    Code JavaScript:
    var multipleValues = $("#multiple").val() || [];

  3. #3
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $("select").change(function () {
    var str = "";
    $("select option:selected").each(function () {
    str += $(this).text() + " ";
    });
    $("div").text(str);
    })
    .trigger('change');
    </script>

  4. #4
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can also do it fairly easily without jquery.
    Code:
    <select name="mySel"  multiple="multiple">
              <option value="val1">option 1</option>
              <option value="val2">option 2</option>
              <option value="val3">option 3</option>
              <option value="val4">option 4</option>
    </select> 
    <button type="button" onclick="showSelectsd();">Show selected values</button>
    
    <script  type="text/javascript">
    
    function showSelectsd(){
        var selO = document.getElementsByName('mySel')[0];
        var selValues = [];
        for(i=0; i < selO.length; i++){
            if(selO.options[i].selected){
                selValues.push(selO.options[i].value);
            }
        }
    }
    </script>


Tags for this Thread

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
  •