php - How do you sort an HTML drop down by the Option text, not the value? -
when user clicks button, i'm loading genres html dropdown using jquery. i'd order drop down in alphabetical order of option text, not value.
my php genre array looks like
$genres = array (0=>"all", 200=>"pop", 201=>"classic rock", ...etc the key numbers loaded values in drop down, while text fills options.
and jquery
$.each(json, function(val, text) { options[options.length] = new option(text,val); }); that displays in drop down
all pop classic rock ie not in alphabetical order
my first plan sort array in php, on testing, if array is
$genres = array (0=>"all", 201=>"classic rock", 200=>"pop", ...etc the drop down still displays
all pop classic rock how force order in order receives json?
thanks help.
try like,
$(function(){ var options = $("#select-box-id option"); options.sort(function(a,b) { if (a.text > b.text) return 1; // .text text comparison else if (a.text < b.text) return -1; else return 0 }); $("#select-box-id").html(options ); });
Comments
Post a Comment