Jana San Jana San - 2 years ago 75
Javascript Question

How to get Chosen Multiselect selected values under the For Loop

This is my view file code

<?php for($i=0; $i<4; $i++) { ?>
<div class="box22">
<div class="mcm">
<input type="text" placeholder="Myself" id="coworkers" name="coworkers[]" />
<span class="bar"></span>

<div class="select2">
<select id="category_<?php echo $i; ?>" name="category[]" class="chosen-select ref-sel1" multiple >
foreach($genre as $gen){
echo '<option value='.$gen->genre_id.'>'.$gen->genre_name.'</option>';
<?php } ?>

my script : when i chose one or more from option, it does not comes into script. How to get multiple values under loop

var form = $(this);
var foo = [];
$('#category :selected').each(function(i, selected){
foo[i] = $(selected).text();

Answer Source

change text to val()

 $('option:selected').each(function(i, selected){


var foo = [];
var temp =[]
     $(v).find('option:selected').each(function(i, selected){

see demo for the second option here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download