Skip to content

Instantly share code, notes, and snippets.

@ssayala
Created December 23, 2011 18:56
Show Gist options
  • Save ssayala/1515068 to your computer and use it in GitHub Desktop.
Save ssayala/1515068 to your computer and use it in GitHub Desktop.
knockout binding to disable and clear an element with value binding
ko.bindingHandlers.disableClear = {
update: function (element, valueAccessor, allBindingsAccessor) {
var value = !ko.utils.unwrapObservable(valueAccessor());
if (value && element.disabled)
element.removeAttribute("disabled");
else if ((!value) && (!element.disabled)) {
//If there is a value binding, that's what we want to clear
var valueBinding = allBindingsAccessor().value;
if ( typeof(valueBinding) === 'function') {
valueBinding('');
}
element.disabled = true;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment