jquery - Parse array into SELECT OPTIONS -
this question has answer here:
i have array this:
["news & politics", "personal journals", "society & culture", "technology", "arts", "natural sciences", "business", "comedy", "history", "tech news", "tv & film", "design", "performing arts", "education", "christianity", "investing", "sports & recreation", "professional", "social sciences", "automotive", "science & medicine", "management & marketing", "sexuality", "philosophy", "self-help", "literature", "buddhism", "places & travel", "music", "language courses", "careers", "food", "kids & family", "national"]
i need "push" these values select
each of items in array being it's own option
.
how go accomplishing this?
the array stored variable all_cat_unique
, (of course) following doesn't work. appends entire array select bunch of times.
$.each(obj.entry, function(i, data) { $('#all-categories').append('<option>'+all_cat_unique+'</option>'); });
thanks.
given array:
var all_cat_unique = ["news & politics", "personal journals", "society & culture", "technology", "arts", "natural sciences", "business", "comedy", "history", "tech news", "tv & film", "design", "performing arts", "education", "christianity", "investing", "sports & recreation", "professional", "social sciences", "automotive", "science & medicine", "management & marketing", "sexuality", "philosophy", "self-help", "literature", "buddhism", "places & travel", "music", "language courses", "careers", "food", "kids & family", "national"];
html select:
<select id="all-categories"></select>
you can use following code dynamically append options array select:
for(var = 0; < all_cat_unique.length; i++) $('#all-categories').append( $("<option></option>").attr("value",all_cat_unique[i]).text(all_cat_unique[i]) );
here working jsfiddle given array. hope helps
Comments
Post a Comment