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
Last updated
Was this helpful?
https://stackoverflow.com/questions/1897129/how-to-convert-unordered-list-into-nicely-styled-select-dropdown-using-jquery
Last updated
Was this helpful?
Asked 10 years, 6 months agoActive Viewed 44k times1817
How do I convert an unordered list in this format
into a dropdown in this format
using jQuery?
In reply to your last comment, I modified it a little bit but haven't tested it. Let me know.
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
This solution is working also in IE and working with selected item (in anchor tag).
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.
The unordered list must be in the form:
To convert the ul to select just call:
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: follow 3,63233 gold badges3030 silver badges4545 bronze badgesasked Dec 13 '09 at 17:36127k211211 gold badges529529 silver badges810810 bronze badges
@Dominic Rodger - The code i given is just for example –
15
follow 3,63233 gold badges3030 silver badges4545 bronze badgesanswered Dec 13 '09 at 19:325,8302323 silver badges2121 bronze badges
code working to make dropdown but links not opening in new window –
link opening in same tab in firefox –
It should only open a new window with a _blank target on the link. Have you dropped a console.log into the if statement to see if it's trying to open in a new window? –
yes your code was right. it was my mistake i had not added target=_blank . now it working fine u can see here thanks for this. –
2Use change(), but on the select element, not the option: select.change(function() { window.location = $(this).find("option:selected").val(); });
<-----this should get the above code to work in Chrome. –
21
There was a bug in my code also, tried to take href from the li element. follow answered Dec 13 '09 at 17:50110k3131 gold badges171171 silver badges179179 bronze badges
You are trying to execute the script inside <style> tags, change those to <script>. I also updated my function, there was a small mistake. –
oh , my mistake , solved now now i added ur latest code it's working now but on select a item from dropdown i wanted to open link in new window –
@Jitendra, opening link on change and styling the select is out of the scope for this question, Google has a lot of resources for both of those. If the code is working, lets just close this one. –
Yes code is working and great thanks for it. I closed the question but if u can give the way to open links in new window then it would be better –
I found a another js solution for same and in this link opens in new window but the problem is it's not jquery based –
2
follow answered Jul 20 '11 at 10:312111 bronze badge
Thanks exactly what I was after –
0
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 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:
follow answered Oct 4 '13 at 18:041122 bronze badges0
Take a look at the code here:
And a live example here:
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. follow answered Apr 1 '17 at 15:0260666 silver badges99 bronze badges