bgrins / spectrum

The No Hassle JavaScript Colorpicker
https://bgrins.github.io/spectrum/
MIT License
2.32k stars 588 forks source link

On change event can we get old color value. #484

Open krishnaisdinesh opened 7 years ago

krishnaisdinesh commented 7 years ago

When move event is fired it internaly fire change but after move complete. So we can get old value form selector and while firing event selected colr is passed. Change event first replace the secltor value then new color is passed. So old color is lost.

Is there anyway to get old( before change) color and new both. I am using input color code text box. So it only fires change event. I amnot able to older code.

Is there any option

jcubic commented 6 years ago

You can use color option in closure:

  $('.color').each(function() {
    var self = $(this);
    var prev_color = '#ff0000';
    self.spectrum({
      color: prev_color,
      palette: [],
      preferredFormat: 'hex',
      selectionPalette: [],
      change: function(color) {
        console.log({color, prev_color}); 
        prev_color = color.toHexString();
      }
    });
  });
});