Sergino
Sergino

Reputation: 10818

Select val and text from all dropdowns

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

Answers (2)

jogesh_pi
jogesh_pi

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);

DEMO

Upvotes: 3

kag
kag

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

Related Questions