Search code examples
javascriptjqueryajaxecmascript-6ecmascript-2016

How to get multiple select box values and texts together using jQuery?


I have a select box and user can select multiple colors so I want to get values and texts of selected options with bellow code I just get texts or just get values but I want both

  selected_colors = $('.selectpicker option:selected')
                .toArray().map(item => item.text);
                console.log(selected_colors);

it gives me this

(2) ["بنفش", "نارنجی روشن"]0: "بنفش"1: "نارنجی روشن"length: 2__proto__: Array(0)

btw "نارنجی روشن" and ... are colors name


Solution

  • Try this below :

    selected_colors = $('.selectpicker option:selected').toArray().map(item => ({'text':item.text, 'value':item.value}));
    
    console.log(selected_colors);