Last active
April 17, 2019 23:18
-
-
Save zeshanshani/1c98692099a6fd25f1e6 to your computer and use it in GitHub Desktop.
Adding class to jQuery SelectBox selected dropdown option for styling purposes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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