javascript - Jquery function to split option list into hierarchical select -
i have list looks this:
<select> <option value="all" selected="selected">- -</option> <option value="16">africa</option> <option value="17">asia</option> <option value="56">-china</option> <option value="57">-japan</option> <option value="19">canada</option> <option value="20">-alberta</option> <option value="21">-british columbia</option> <option value="22">-manitoba</option> <option value="23">-new brunswick</option> <option value="24">-newfoundland & labrador</option> <option value="25">-northwest territories</option> <option value="26">-nova scotia</option> <option value="27">-nunavut</option> <option value="28">-ontario</option> <option value="29">-prince edward island</option> <option value="30">-quebec</option> <option value="31">-saskatchewan</option> <option value="32">-yukon</option> <option value="33">central & south america</option> <option value="34">europe</option> <option value="35">republic of ireland</option> <option value="36">united arab emirates</option> <option value="37">united kingdom</option> <option value="38">-england</option> <option value="39">-northern ireland</option> <option value="40">-scotland</option> <option value="41">-wales</option> </select>
i can't change html, need split select 2 selects jquery first show top level choices example if canada chosen show second drop down provinces. has dynamic underlying list might change time.
i understand easier if there optgroups unfortunately out of control. basicly need convert simple list hierarchical select in browser.
you can use below query split continents , countries select box.
note - put id="select"
main select box.
$(function(){ var continent=''; $('#select option:gt(0)').each(function(){ var value = $(this).val(); //check if option text don't have '-' in it, take // continent , create select box same id if($(this).text().indexof("-")==-1 && continent!=value) { continent=value; $('#select').after('<select id="'+continent+'" style="display:none" class="country"></select>'); } else { //add option newly created select box $('#'+continent).append($(this)); } }); //remove country select box empty $('.country').filter(function(){ return $(this).children().length ==0; }).remove(); //bind change event select box show / hide country select box $('#select').change(function(){ $('.country').hide(); $('#'+$(this).val()).show(); }); });
Comments
Post a Comment