How to get all selected option text form multi select Using Javascript
// Return an array of the selected opion values
// select is an HTML select element
function GetSelectValues(select) {
var result = [];
var options = select && select.options;
var opt;
for (var i=0, iLen=options.length; i<iLen; i++) {
opt = options[i];
if (opt.selected) {
result.push(opt.value || opt.text);
}
}
return result;
}
pass this function your select box as like..
var selectBox = document.getElementsById('fm_delivery_or_collection');
alert(GetSelectValues(selectBox ));
it will return the array of selected values
By using Jquery
$('select#fm_delivery_or_collection').val()
the val
function called from the select
will return an array if its a multiple.
Your comment please suggest me how can i get all selected option text
, So you can try this:
$("#fm_delivery_or_collection option:selected").each(function () {
var $this = $(this);
if ($this.length) {
var selText = $this.text();
console.log(selText);
}
});
do all in one line now
var text = $('#selector option:selected').toArray().map(item => item.text).join();
it return like this:
text1,text2,text3,text4
Try with this
$("#fm_delivery_or_collection option").each(function(){
if($(this).attr('selected') == 'selected')
{
var name = $("#fm_delivery_or_collection").attr('name')
var str1 = $("#fm_delivery_or_collection").attr('id').replace("fm_"," ")
requestString += "<b>"+str1.replace(/_/g," ")+"</b>" + ':' +strUser+"<br>";
}
})