Skip to content

Instantly share code, notes, and snippets.

@MrMaksimize
Created February 14, 2012 15:25
Show Gist options
  • Save MrMaksimize/1827546 to your computer and use it in GitHub Desktop.
Save MrMaksimize/1827546 to your computer and use it in GitHub Desktop.
menu to dropdown
//from https://github.com/MrMaksimize/DC2011/blob/mobile/sites/all/themes/chicago_2011/js/mobile_grid_response.js
function menuToDropdown(topContainer, ulClass, context){
if (context == 'mobile'){
$("<select />").appendTo(topContainer).addClass('mobile mobile-dropdown ' + topContainer.replace('#','') + '-mobile');
$('ul'+ulClass + ' li', topContainer).each( function(){
var el = this;
//construct optionString
var optionString = '<option value = "' + $('a', this).attr('href') + '" ';
if($(this).hasClass('active')){
optionString = optionString + 'selected="selected" '
}
optionString = optionString + '>' + $('a', this).text() + '</a>';
$('select', topContainer).append(optionString);
/*append('<option value = "' +
$('a', this).attr('href') + '">' +
$('a', this).text() + '</a>');*/
});
//dc2011 hack
//find the secondary menu from the top
$('#block-menu-secondary-links ul li').each( function(){
var el = this;
var optionString = '<option value = "' + $('a', this).attr('href') + '" ';
if($('a', el).hasClass('active')){
optionString = optionString + 'selected="selected" '
}
optionString = optionString + '>' + $('a', this).text() + '</a>';
$('select', topContainer).append(optionString);
});
//now hide the original lis for future use
$('ul'+ulClass).hide();
//bind the change function
$('select', topContainer).change(function(){
window.location = $(this).find("option:selected").val();
});
}
else{
$('ul'+ulClass).show();
$('select', topContainer).remove();
}
}
function menuToDropdown(topContainer, ulClass, context){
if (context == 'mobile'){
$("<select />").appendTo(topContainer).addClass('mobile mobile-dropdown ' + topContainer.replace('#','') + '-mobile');
$('ul'+ulClass + ' li', topContainer).each( function(){
var el = this;
//construct optionString
var optionString = '<option value = "' + $('a', this).attr('href') + '" ';
if($(this).hasClass('active')){
optionString = optionString + 'selected="selected" '
}
optionString = optionString + '>' + $('a', this).text() + '</a>';
$('select', topContainer).append(optionString);
/*append('<option value = "' +
$('a', this).attr('href') + '">' +
$('a', this).text() + '</a>');*/
});
//now hide the original lis for future use
$('ul'+ulClass).hide();
//bind the change function
$('select', topContainer).change(function(){
window.location = $(this).find("option:selected").val();
});
}
else{
$('ul'+ulClass).show();
$('select', topContainer).remove();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment