Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Using the
.val()
function on a multi-select list will return an array of the selected values:
var selectedValues = $('#multipleSelect').val();
and in your html:
<select id="multipleSelect" multiple="multiple">
<option value="1">Text 1</option>
<option value="2">Text 2</option>
<option value="3">Text 3</option>
</select>
–
–
–
–
–
var select_button_text = $('#SelectQButton option:selected')
.toArray().map(item => item.text);
Output: ["text1", "text2"]
var select_button_text = $('#SelectQButton option:selected')
.toArray().map(item => item.value);
Output: ["value1", "value2"]
If you use .join()
var select_button_text = $('#SelectQButton option:selected')
.toArray().map(item => item.text).join();
Output: text1,text2,text3
var selected=[];
$('#multipleSelect :selected').each(function(){
selected[$(this).val()]=$(this).text();
console.log(selected);
Yet another approch to this problem. The selected array will have the indexes as the option values and the each array item will have the text as its value.
for example
<select id="multipleSelect" multiple="multiple">
<option value="abc">Text 1</option>
<option value="def">Text 2</option>
<option value="ghi">Text 3</option>
</select>
if say option 1 and 2 are selected.
the selected array will be :
selected['abc']=1;
selected['def']=2.
<select id="multiple" multiple="multiple" name="multiple">
<option value=""> -- Select -- </option>
<option value="1">Opt1</option>
<option value="2">Opt2</option>
<option value="3">Opt3</option>
<option value="4">Opt4</option>
<option value="5">Opt5</option>
</select>
JQuery Code:
$('#multiple :selected').each(function(i, sel){
alert( $(sel).val() );
Hope it works
–
–
–
var Accessids = "";
$(".multi_select .btn-group>ul>li input:checked").each(function(i,obj)
Accessids=Accessids+$(obj).val()+",";
Accessids = Accessids.substring(0,Accessids.length - 1);
console.log(Accessids);
In case if you have multiple select boxes on a single page and they all have the same class which you can prefer in case of multiple rather than tracking id's:
$('.classname option:selected').map(function(){
return this.value; // If you want value.
// Or you could also do.
return this.text; // If you want text of select boxes.
}).get(); // It will return an Array of selected values/texts.
This jQuery works well for me. I use a getFormValues() function for various tasks, so it's a good place to rebuild some of the form data when it's missing.
It checks each form field to see if it's a multi-select type and if it is, rebuild the value with all of the selected options. I'm sure something similar would be needed for checkboxes too...
// Get form values as a key-value Object
function getFormValues(form) {
var formData = new FormData(form[0]),
values = Object.fromEntries(formData);
// Rebuild multi-select values.
$.each(values, function(key, value){
var element = $(form).find(':input[name="'+key+'"]');
if (element.is('select[multiple]')){
values[key] = element.val();
return values;
// Use with AJAX.
$('form').on('submit', function(e){
e.preventDefault();
var form = $(this);
$.ajax({
URL: 'http://google.com/',
data: getFormValues(form),
success: function(data, textStatus){
console.log(data);
error: function(jqXHR, textStatus, errorThrown){
console.log(textStatus, errorThrown);
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.