0

I have multiple select in which I want to clicked option on which I cliked.

E.g. If I click on first option then display first option index or value.

HTML Code

<select class="founder_designation" rel="Designation" placeholder="Designation" multiple="multiple" name="designation">
<option value="">-- Select Designation --</option>
<option value="chairman_managing_director">Chairman &amp; Managing Director</option>
<option selected="selected" value="director" >Director</option>
<option value="whole_time_director">Whole Time Director</option>
<option value="other">Other</option>
</select>

jQuery Code

$(".founder_designation").on("change",function(){
  alert($(this).val());   //It display all selected options with comma separated
})

JS Fiddle

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122

4 Answers4

2

this will do the trick,

$('.founder_designation').on('click','option',function(){
 alert($(this).val());
});

if you want all the value then try some thing like this,

$('.founder_designation').on('click','option',function(){

    alert("clicked value:"+$(this).val());

    $a=$('.founder_designation option:selected');
    $str='';
    for($i=0;$i<$a.length;$i++){
        $str+=$('.founder_designation option:selected:eq('+$i+')').val()+",";
    }

    alert("all selected :"+$str);
});

Check the demo JS FIDDLE DEMO

Domain
  • 11,562
  • 3
  • 23
  • 44
1

This should do the trick:

$('.founder_designation option:selected').val();

If you have several selects with the classname founder_designation you could do this:

$('.founder_designation option').click(function(e) {
   var value = $(this).val();
   alert(value);
});
JasonK
  • 5,214
  • 9
  • 33
  • 61
1
$(".founder_designation").on("change",function(e){
  alert(e.target.value);
  // OR
  alert($(e.target).val());
});
Janne L
  • 11
  • 2
0
let previouslySelected = []; // declare as a global array

$("#your-select-picker-id").change(function () {

    let currentlySelected   = $(this).val(); // ids [1, 2, 3]
    let isAdding            = currentlySelected.length > previouslySelected.length;

    if ( isAdding )
    {
        lastSelectedId = currentlySelected.find( ( element ) => ! previouslySelected.includes(element) )
    }
    else
    {
        lastSelectedId = previouslySelected.find( ( element ) => ! currentlySelected.includes(element) )
    }
    previouslySelected = currentlySelected;
});
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49