Skip to content

Instantly share code, notes, and snippets.

@rhcarlosweb
Created August 17, 2017 21:22
Show Gist options
  • Save rhcarlosweb/9a96393bc38ef2bffb11f1ffbf2bfb75 to your computer and use it in GitHub Desktop.
Save rhcarlosweb/9a96393bc38ef2bffb11f1ffbf2bfb75 to your computer and use it in GitHub Desktop.
Convert ul li on dropdown
function listToDropdown(selector) {
var $list = $(selector),
$select = $('<select class="btn btn-outline btn-white select-field-menup" />');
// blank
$select.append($('<option />').attr('value', '').html('Navegação'));
// for each list change to a option in select field
$list.children('li').each(function(index, el) {
// extract url from link
urlLink = $(this).children('a').attr('href');
// extra text link
textLink = $(this).children('a').text();
$select.append($('<option />').attr('value', urlLink).html(textLink));
});
$list.replaceWith($select);
}
$('.select-field-menup').change(function(event) {
var url = $(this).val(); // get selected value
if (url) { // require a URL
window.location = url; // redirect
}
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment