Skip to content

Instantly share code, notes, and snippets.

@zeshanshani
Last active April 17, 2019 23:18
Show Gist options
  • Save zeshanshani/1c98692099a6fd25f1e6 to your computer and use it in GitHub Desktop.
Save zeshanshani/1c98692099a6fd25f1e6 to your computer and use it in GitHub Desktop.
Adding class to jQuery SelectBox selected dropdown option for styling purposes
// Improved code using selectBox callback fucntions.
$("select").selectbox({
onOpen: function(inst) {
var $this = $(this),
thisAnchor = $this.siblings('.sbHolder').find('.sbOptions').find('a[href="#' + $this.val() + '"]');
thisAnchor.closest('li').addClass('active').siblings('li').removeClass('active');
},
onClose: function(inst) {
var $this = $(this),
thisAnchor = $this.siblings('.sbHolder').find('.sbOptions').find('a[href="#' + $this.val() + '"]');
thisAnchor.closest('li').addClass('active').siblings('li').removeClass('active');
}
});
// This script is by Zeshan Ahmed @zeshanshani
// The script will add a class of "sbActive" to the jQuery SelectBox plugin selected dropdown option for styling purpose
// Plugin URL: http://www.bulgaria-web-developers.com/projects/javascript/selectbox/
$('.sbOptions li:first-child').addClass('active');
$('.sbOptions li a').click(function() {
var $this = $(this);
$this.addClass('sbActive');
$this.closest('li').siblings().children('a').removeClass('sbActive');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment