0

In an php, we can use select option in that a user can select more than one option value, after that i click the move button, a on-click function is wrote in button, now how can i get all selected option value in single onclick.

 <button type="button" id="search_rightSelected" onclick="inventory_left_to_right(document.getElementById('search'),false)" class="btn btn-block"><i class="glyphicon glyphicon-chevron-right"></i></button>
KarnaGowtham
  • 95
  • 2
  • 14

2 Answers2

2
 <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.0.min.js"></script>

 <select id="search" multiple>
   <option value="1">Volvo</option>
   <option value="2">Saab</option>
   <option value="3">Opel</option>
   <option value="4">Audi</option>
</select> 

<button type="button" id="search_rightSelected" onclick="inventory_left_to_right()" class="btn btn-block"><i class="glyphicon glyphicon-chevron-right"></i> HHH </button>

<script>

function inventory_left_to_right() {
    //var allVal = '';
    var allValArr = [];
    $("#search :selected").each(function() {
       // you can use to get the values $(this).val();
       //allVal += $(this).val()+',';
       allValArr.push($(this).val());
    });
    //allVal = allVal.substring(0, allVal.length - 1);
    console.log(allValArr);
}
</script>
MD. ABDUL Halim
  • 712
  • 3
  • 11
  • 32
0

You can try this: DEMO

$('#search_rightSelected').click(function() {
     var foo = []; 
     $('#multiple :selected').each(function(i, selected){ 
     foo[i] = $(selected).text(); 
   });
});

OR

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

kishan Radadiya
  • 788
  • 6
  • 14