JSFiddle - React, Tailwind, and code Playground
by nadeemmnn2007
HTML
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<body>
<form>
<label>Country:</label>
<select class="country" multiple="multiple" size="5">
<option>United States</option>
<option>India</option>
<option>United Kingdom</option>
<option>Brazil</option>
<option>Germany</option>
</select>
<button type="button" onclick="getValues();">Get Values</button>
</form>
</body>
JavaScript
var arr=[];
var selected;
$(document).ready(function() {
$("select.country").click(function(){
var latestSelected;
if (selected) {
var currentOptionsValue = $(this).val();
latestSelected = currentOptionsValue.filter(function(element) {
return selected.indexOf(element) < 0;
});
}
if(!selected){
arr.push($(this).val());
}
else{
arr.push(latestSelected);
}
selected = $(this).val();
});
});
function getValues(){
alert(arr.join(','));
}