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 QuestionAsked 10 years, 6 months agoActive 3 years, 3 months agoViewed 44k times1817
How do I convert an unordered list in this format
into a dropdown in this format
using jQuery?
@Dominic Rodger - The code i given is just for example – Jitendra Vyas Dec 13 '09 at 17:49
5 Answers
In reply to your last comment, I modified it a little bit but haven't tested it. Let me know.
code working to make dropdown but links not opening in new window – Jitendra Vyas Dec 14 '09 at 4:46
link opening in same tab in firefox – Jitendra Vyas Dec 14 '09 at 4:46
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? – czarchaic Dec 14 '09 at 5:12
yes your code was right. it was my mistake i had not added target=_blank . now it working fine u can see here jsbin.com/agane thanks for this. – Jitendra Vyas Dec 14 '09 at 5:45
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. – The Pied Pipes Jul 12 '14 at 21:24
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
You are trying to execute the script inside <style> tags, change those to <script>. I also updated my function, there was a small mistake. – Tatu Ulmanen Dec 13 '09 at 18:20
oh , my mistake , solved now now i added ur latest code it's working now jsbin.com/igano but on select a item from dropdown i wanted to open link in new window – Jitendra Vyas Dec 13 '09 at 18:27
@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. – Tatu Ulmanen Dec 13 '09 at 18:38
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 – Jitendra Vyas Dec 13 '09 at 18:42
I found a another js solution for same onlinetools.org/tests/enhancedropdown.html and in this link opens in new window but the problem is it's not jquery based – Jitendra Vyas Dec 13 '09 at 18:44
This solution is working also in IE and working with selected item (in anchor tag).
Thanks exactly what I was after – Leigh Aug 18 '16 at 15:30
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 csswatch 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:
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 Code
And a live example here: Code Example
The unordered list must be in the form:
To convert the ul to select just call:
Last updated