Skip to content

Instantly share code, notes, and snippets.

@dzerrenner
Created May 21, 2014 21:06
Show Gist options
  • Save dzerrenner/561bde7a2c97ab95d699 to your computer and use it in GitHub Desktop.
Save dzerrenner/561bde7a2c97ab95d699 to your computer and use it in GitHub Desktop.
Knockout.js custom binding handler to show / hide an element with jQuery's slideUp / slideDown methods
ko.bindingHandlers.slideToggleVisible = {
init: function(element, valueAccessor) {
var value = valueAccessor();
ko.unwrap(value) ? $(element).show() : $(element).hide();
},
update: function(element, valueAccessor) {
var value = valueAccessor();
ko.unwrap(value) ? $(element).slideDown() : $(element).slideUp();
}
};
@dzerrenner
Copy link
Author

Usage:

<div data-bind="slideToggleVisible: expanded">

while expanded is an observable returning the current visible state of the element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment