javascript - How to convert unordered list into dropdown using jquery? -



javascript - How to convert unordered list into <select> dropdown using jquery? -

how convert unordered list in format

class="snippet-code-html lang-html prettyprint-override"><ul class="selectdropdown"> <li><a href="one.html" target="_blank">one</a></li> <li><a href="two.html" target="_blank">two</a></li> <ul> <li><a href="twoone.html" target="_blank">two one</a></li> <ul> <li><a href="twooneone.html" target="_blank">two 1 one</a></li> </ul> <li><a href="twotwo.html" target="_blank">twotwo</a></li> </ul> <li><a href="three.html" target="_blank">three</a></li> <li><a href="four.html" target="_blank">four</a></li> <li><a href="five.html" target="_blank">five</a></li> <li><a href="six.html" target="_blank">six</a></li> <li><a href="seven.html" target="_blank">seven</a></li> </ul>

i want convert construction <select> structure

jquery code

jquery('.selectdropdown li a').each(function() { var el = jquery(this); jquery('<option />', { "value" : el.attr('href'), "text" : el.text() }).appendto('#top select'); }); if (jquery('.selectdropdown li a').length < 1) jquery('#top').hide(); jquery('#top').find('select').change(function() { window.location = jquery(this).find('option:selected').val(); });

html

<div id="top"> <select></select> </div>

here fiddle fiddle link

javascript jquery html css html5

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -