I'm trying to convert (with jQuery) a multi-level UL
into a SELECT
dropdown with the nested UL
group being wrapped in OPTGROUPS
s. I'm messing around with the idea of using this technique to make responsive site menus (think drop-down menus).
I've converted list items into a option dropdown in the past, but never with optgroups, and I can't figure it out.
An example of my UL
structure;
<ul id="sitemap">
<li><a href="www.google.com">Home</a></li>
<li><a href="www.google.com">Small Business</a>
<ul>
<li><a href="www.google.com">Laptops</a></li>
<li><a href="www.google.com">Workstations</a></li>
<li><a href="www.google.com">Workstations</a></li>
<li><a href="www.google.com">Printers</a></li>
<li><a href="www.google.com">Mobile Phones</a></li>
</ul>
</li>
<li><a href="www.google.com">Public Sector</a>
<ul>
<li><a href="www.google.com">State Government</a></li>
<li><a href="www.google.com">Federal Government</a></li>
<li><a href="www.google.com">Support and Drivers</a></li>
</ul>
</li>
<li><a href="www.google.com">Large Enterprise</a>
<ul>
<li><a href="www.google.com">Services</a></li>
<li><a href="www.google.com">Solutions</a></li>
</ul>
</li>
</ul>
Example of what I what it to become;
<select id="sitemap">
<option href="www.google.com">Home</option>
<optgroup label="Small Business">
<option href="www.google.com">Laptops</option>
<option href="www.google.com">Workstations</option>
<option href="www.google.com">Printers</option>
<option href="www.google.com">Mobile Phones</option>
</optgroup>
<optgroup label="Public Sector">
<option href="www.google.com">State Government</option>
<option href="www.google.com">Federal Government</option>
<option href="www.google.com">Support and Drivers</option>
</optgroup>
<optgroup label="Large Enterprise">
<option href="www.google.com">Services</option>
<option href="www.google.com">Solutions</option>
</optgroup>
</select>
It doesn't need to go deeper than one level - and I'm pretty sure optgroups don't really work so well that deep anyways. Any assistance you can contribute would be very welcome. Thanks.