views:

350

answers:

2

In the code below, how to get the values of multiselect box in function val() using jquery or 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;">  

Thanks..

+2  A: 

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.

prodigitalson
The multiselect box is dynamically generated as u can see ,so when i say alert($('select#my_multiselect').val()); it says undefined....
Hulk
Got it Thanks.........
Hulk
Just for clarification that was an example selector - you would need to figure out what selector to used based on your dom/markup.
prodigitalson
+2  A: 

This doc is like a holy grail for select and jquery. http://comp345.awardspace.com/select_element_cheatsheet.pdf

Aseem Gautam
Cool...........................................
Hulk