Skip to content

Instantly share code, notes, and snippets.

@Kcko
Created November 21, 2018 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kcko/173e16b9995af1f2e5863bfda8950152 to your computer and use it in GitHub Desktop.
Save Kcko/173e16b9995af1f2e5863bfda8950152 to your computer and use it in GitHub Desktop.
<select></select>
<nav>
<ul>
<li><a href="">A</a></li>
<li><a href="">B</a>
<ul>
<li><a href="">A</a></li>
<li><a href="">B</a></li>
<li><a href="">C</a>
<ul>
<li><a href="">A</a></li>
<li><a href="">B</a></li>
<li><a href="">C</a></li>
<li><a href="">D</a></li>
<li><a href="">E</a></li>
</ul>
</li>
<li><a href="">D</a></li>
<li><a href="">E</a></li>
</ul>
</li>
<li><a href="">C</a></li>
<li><a href="">D</a></li>
<li><a href="">E</a></li>
</ul>
</nav>
$(function(){
var select = $("select");
$("nav a").each(function(){
var $this = $(this);
var text = $this.text();
var level = $this.parents("ul").length;
var indent = '';
if (level > 1)
{
indent = str_repeat("\u2013", level);
}
select.append('<option>'+indent + text+'</option>');
});
});
function str_repeat (input, multiplier) {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
// + improved by: Ian Carter (http://euona.com/)
// * example 1: str_repeat('-=', 10);
// * returns 1: '-=-=-=-=-=-=-=-=-=-='
var y = '';
while (true) {
if (multiplier & 1) {
y += input;
}
multiplier >>= 1;
if (multiplier) {
input += input;
}
else {
break;
}
}
return y;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment