Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basic Knockout.js 'hidden' binding. Uses jQuery.
/**
* Knockout Extension: Hidden
*
* The inverse of the `visible` binding: hides the element when the observable
* is true, shows it when the observable is false.
*
* @requires Knockout.js
* @requires jQuery
* @author Jon Stout (www.jonstout.net)
*/
'use strict';
(function(ko, $) {
if (typeof ko.bindingHandlers.hidden === 'undefined') {
ko.bindingHandlers.hidden = {
update: function(element, valueAccessor) {
var isHidden = ko.unwrap(valueAccessor());
if (isHidden) {
$(element).hide();
} else {
$(element).show();
}
}
};
}
})(ko, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment