Skip to content

Instantly share code, notes, and snippets.

@greggnakamura
Last active September 1, 2016 17:49
Show Gist options
  • Save greggnakamura/f290caf27b7e1958b8ba0bc43ae2af14 to your computer and use it in GitHub Desktop.
Save greggnakamura/f290caf27b7e1958b8ba0bc43ae2af14 to your computer and use it in GitHub Desktop.
jQuery: Create dropdown list; redirect on selection
(function(d, $) {
load();
$('#dropDown').on('change', function(e) {
e.preventDefault();
var selectedItem = $(this).find(":selected").val();
if (selectedItem !== null) {
d.location.href = selectedItem;
}
});
function load() {
var partDItems = $('ul > li > a'),
$selectEl = $('<select id="dropDown" class="select">');
$firstOption = $('<option>-- Select --</option>');
$selectEl.append($firstOption);
$.each(partDItems, function() {
var $title = $(this).text().trim();
var $link = $(this).attr('href');
var $optionEl = $('<option>', {
text: $title,
value: $link,
});
$selectEl.append($optionEl);
});
$('.select-container').append($selectEl);
}
})(document, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment