Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value of multiselect box using jQuery or pure JS

In the code shown below, how to get the values of multiselect box in function val() using jQuery or pure JavaScript?

<script>     function val() {         //Get values of mutliselect drop down box     }      $(document).ready(function () {         var flag = 0;         $('#emp').change(function () {             var sub = $("OPTION:selected", this).val()             if (flag == 1) $('#new_row').remove();             $('#topics').val('');             var html = '<tr id="new_row" class="new_row"><td>Topics:</td><td>  <select    id="topic_l" name="topic_l" class="topic_l" multiple="multiple">';             var idarr = new Array();             var valarr = new Array(); { %                 for top in dict.tops %             }             idarr.push('{{top.is}}');             valarr.push('{{pic.ele}}'); { % endfor %             }             for (var i = 0; i < idarr.length; i++) {                 if (sub == idarr[i]) {                     html += '<option value="' + idarr[i] + '" >' + valarr[i] + '</option>';                 }             }             html += '</select></p></td></tr>';             $('#tops').append(html);             flag = 1;         });     }); </script> Emp:&nbsp;&nbsp;&nbsp;&nbsp; <select id="emp" name="emp">     <option value=""></option>     <option value="1">1</option> </select>  <div name="tops" id="tops"></div>  <input type="submit" value="Create Template" id="create" onclick="javascript:var ret=val();return ret;"> 
like image 860
Hulk Avatar asked Mar 30 '10 06:03

Hulk


People also ask

How can get value from multiselect in jQuery?

With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.

What is Multiselect in jQuery?

multiSelect is a jQuery plugin for converting an DIV element into a select list which allows you to select multiple items from a dropdown list as like a tag/token input.


2 Answers

the val function called from the select will return an array if its a multiple. $('select#my_multiselect').val() will return an array of the values for the selected options - you dont need to loop through and get them yourself.

like image 157
prodigitalson Avatar answered Oct 13 '22 19:10

prodigitalson


I think the answer may be easier to understand like this:

$('#empid').on('change',function() {    alert($(this).val());    console.log($(this).val());  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>  <select id="empid" name="empname" multiple="multiple">    <option value="0">Potato</option>    <option value="1">Carrot</option>    <option value="2">Apple</option>    <option value="3">Raisins</option>    <option value="4">Peanut</option>  </select>  <br />  Hold CTRL / CMD for selecting multiple fields

If you select "Carrot" and "Raisins" in the list, the output will be "1,3".

like image 35
Etdashou Avatar answered Oct 13 '22 21:10

Etdashou