Reputation: 2915
Is there any other better way to fill up the array like :
var arr = [];
var i = 0;
$('select').children('option').each( function() {
arr[i++] = $(this).html();
});
Upvotes: 11
Views: 20629
Reputation: 15338
using push()
:
var arr = [];
$('select').children('option').each( function() {
arr.push($(this).html());
});
Upvotes: 11
Reputation: 3871
Array.prototype.fill = function(value, length){
while(length--){
this[length] = value;
}
return this;
}
// example:
var coords = [].fill(0, 3);
Upvotes: 0
Reputation: 87073
var arr = [];
$('select option').html(function(i, html) {
arr.push(html);
});
Upvotes: 2
Reputation: 145388
You can use map
method:
var arr = $("select > option").map(function() {
return this.innerHTML;
}).get();
DEMO: http://jsfiddle.net/UZzd5/
Upvotes: 14