Color palette knockout custom binding

In this post I use a wonderful color picker by @lakek.

Color picker

HTML:

<input type="text" data-bind="colorPicker:color" />

Javascript:

ko.bindingHandlers.colorPicker = {
  init: function(element, valueAccessor) {
    var value = valueAccessor();
    $(element).val(ko.utils.unwrapObservable(value));
    $(element).colorPicker();
    $(element).change(function() { value(this.value); });
  },
  update: function(element, valueAccessor) {
    $(element).val(ko.utils.unwrapObservable(valueAccessor()));
  }
}

Reference: KnockoutJs custom binding

Enjoy!


2 thoughts on “Color palette knockout custom binding

    1. Thank you internet for providing the exact answer to the question I did not even know how to ask!

      I offer my humble click-throughs to appease you, in the hope that you may favour me with further precise responses to obscure coding problems in the future.

      All hail!

      @gavjackson – how did you know? Amazing 🙂

Leave a comment