Reputation: 10818
I want to select the val and text from all dropdowns on the page:
Here is the code that gives me all vals
var selected = $('select[name^=dropdown_').map(function () {
if ($(this).val())
return $(this).val();
}).get();
how can I get the selected text as well? So in result I would get an array of pair object.val
and object.text
;
Upvotes: 0
Views: 42
Reputation: 9782
Take a blank array and Store all values and selected text in object:
var _select = [];
$('select[name^=dropdown_').each( function () {
var _obj = {};
_obj.val = $(this).val();
_obj.text = $('option:selected', this).text();
_select.push(_obj);
});
console.log(_select);
Upvotes: 3
Reputation: 144
var result = [];
$("select").each(function(){
var text = this.value;
var id = $( this ).attr( "id" );
result.push({text, id });
});
console.info( result );
Upvotes: 0