Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dryan
Created May 24, 2010 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dryan/412126 to your computer and use it in GitHub Desktop.
Save dryan/412126 to your computer and use it in GitHub Desktop.
convert nav links to <select>
var nav = document.getElementById('nav'),
links = nav.getElementsByTagName('a'),
select = document.createElement('select'),
option = document.createElement('option');
for (var i = 0; i < links.length; i++){
var link = links[i],
_option = option.cloneNode(false);
_option.value = link.href; // Set the option's value to the original link
_option.innerHTML = link.innerHTML; // Set the option's text to the original text
// if this link matches the current URL, add a selected attribute
if( window.location.href == link.href ) {
_option.selected = 'selected';
}
select.appendChild(_option);
};
select.addEventListener('change', function() {
window.location = select.value;
}, false);
nav.parentNode.insertBefore(select, nav);
nav.style.display = 'none';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment