spectrum icon indicating copy to clipboard operation
spectrum copied to clipboard

change does not contain the clicked DOM element

Open maximilianh opened this issue 8 years ago • 4 comments

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?

maximilianh avatar Jan 12 '17 00:01 maximilianh

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.

PreVaDu avatar Feb 21 '17 19:02 PreVaDu

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.

vitorhsb avatar Apr 10 '17 17:04 vitorhsb

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 avatar Apr 10 '17 19:04 maximilianh

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

maximilianh avatar May 10 '18 18:05 maximilianh