bgrins / spectrum

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

change does not contain the clicked DOM element #461

Open maximilianh opened 7 years ago

maximilianh commented 7 years ago

I have 50 color pickers in my application, created in a for-loop. It would be nice if the change event handler could find out from which DOM element (= which color picker) the event is coming. Currently, all the change handler gets is the color. This could be easily hacked to the current system by adding a _target attribute to the color or maybe cleaner by throwing another event, changeElement or something similar.

But maybe I'm missing something, is there an easy solution for this problem with the current color picker?

PreVaDu commented 7 years ago

If I'm right, you set change event function as option by init. It means you have to know current DOM elm, which you set spectrum for, don't you? Than it's easy to use current elm right in change fallback.

vitorhsb commented 7 years ago

I found by experience that the this object inside the change event handler is the input DOM element. You can use this to reference the current color picker.

If you want to access the spectrum API methods inside the event handlers callback, you can access by $(this).spectrum('desired method').

I believe this could be add to the documentation page in order to help others like me and you.

maximilianh commented 7 years ago

Yes it would be great if "this" could be documented. I worked around it with a cludge but would have loved to use it.

maximilianh commented 6 years ago

Came across the problem again, found this answer, was happy to find it, then discovered my own comment from a year ago. Stupid brains.