How to convert unordered list into nicely styled <select> dropdown using jquery? (ok)

https://stackoverflow.com/questions/1897129/how-to-convert-unordered-list-into-nicely-styled-select-dropdown-using-jquery

Ask Questionarrow-up-rightAsked 10 years, 6 months agoActive 3 years, 3 months agoarrow-up-rightViewed 44k times1817

How do I convert an unordered list in this format

<ul class="selectdropdown">
    <li><a href="one.html" target="_blank">one</a></li>
    <li><a href="two.html" target="_blank">two</a></li>
    <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>

into a dropdown in this format

<select>
    <option value="one.html" target="_blank">one</option>
    <option value="two.html" target="_blank">two</option>
    <option value="three.html" target="_blank">three</option>
    <option value="four.html" target="_blank">four</option>
    <option value="five.html" target="_blank">five</option>
    <option value="six.html" target="_blank">six</option>
    <option value="seven.html" target="_blank">seven</option>
</select>

using jQuery?

Edit: When selecting an entry from the select/dropdown the link should open in a new window or tab automatically. I also want to able to style it like: http://www.dfc-e.com/metiers/multimedia/opensource/jqtransform/arrow-up-rightjavascriptarrow-up-right jqueryarrow-up-right cssarrow-up-right xhtmlarrow-up-rightsharearrow-up-right improve this questionarrow-up-right follow edited Mar 24 '14 at 14:13arrow-up-rightarrow-up-rightlorem monkeyarrow-up-right3,63233 gold badges3030 silver badges4545 bronze badgesasked Dec 13 '09 at 17:36arrow-up-rightJitendra Vyasarrow-up-right127k211211 gold badges529529 silver badges810810 bronze badges

add a commentarrow-up-right

5 Answers

Activearrow-up-rightOldestarrow-up-rightVotesarrow-up-right15

In reply to your last comment, I modified it a little bit but haven't tested it. Let me know.

sharearrow-up-right improve this answerarrow-up-right follow edited Mar 24 '14 at 14:19arrow-up-rightarrow-up-rightlorem monkeyarrow-up-right3,63233 gold badges3030 silver badges4545 bronze badgesanswered Dec 13 '09 at 19:32arrow-up-rightczarchaicarrow-up-right5,8302323 silver badges2121 bronze badges

show 4 more commentsarrow-up-right21

EDIT

As with any jQuery code you want to run on page load, you have to wrap it inside $(document).ready(function() { ... }); block, or inside it's shorter version $(function() { ... });. I updated the function to show this.

EDIT

There was a bug in my code also, tried to take href from the li element.sharearrow-up-right improve this answerarrow-up-right follow edited Dec 13 '09 at 18:19arrow-up-rightanswered Dec 13 '09 at 17:50arrow-up-rightTatu Ulmanenarrow-up-right110k3131 gold badges171171 silver badges179179 bronze badges

show 1 more commentarrow-up-right2

This solution is working also in IE and working with selected item (in anchor tag).

sharearrow-up-right improve this answerarrow-up-right follow answered Jul 20 '11 at 10:31arrow-up-rightRadimarrow-up-right2111 bronze badge

add a commentarrow-up-right0

Thank you all for posting the codes. My scenario is similar but my situation is for Responsiveness that for x-size it would switch to a dropdown list, then if not x-size, using csswatcharrow-up-right to check for the "display" properties of an element that has certain amount of width set to it (eg: 740px). Thought I share this solution for anyone who is interested. This is what I have combined with Tatu' codes. Instead of replacing the html, I created then hide the new html then only add them when necessary:

sharearrow-up-right improve this answerarrow-up-right follow edited Oct 4 '13 at 22:05arrow-up-rightanswered Oct 4 '13 at 18:04arrow-up-rightMonarrow-up-right1122 bronze badgesadd a commentarrow-up-right0

I have recently created a solution where the ul transformed, mimics nearly completely the select.

It has in adition a search for the options of the select and supports the active state. Just add a class with name active and that option will be selected.

It handles the keyboard navigation.

Take a look at the code here: GitHub Codearrow-up-right

And a live example here: Code Examplearrow-up-right

The unordered list must be in the form:

To convert the ul to select just call:

Where #id is an id for the unordered list and 767 is the minimum width of the window for the convertion to take place. This is very useful if you want the convertion to take place only for mobile or tablet.sharearrow-up-right improve this answerarrow-up-right follow edited Apr 1 '17 at 15:20arrow-up-rightanswered Apr 1 '17 at 15:02arrow-up-rightquAntonarrow-up-right60666 silver badges99 bronze badgesadd a commentarrow-up-right

Last updated