jquerypickerspectrum

jQuery spectrum color picker


I am using jquery spectrum color picker. Is there a way to get spectrum selector within the change event?

If I apply spectrum to multiple elements by class (hap-ch), can I get active selector instance?

<input id="playerBgColor" class="hap-ch">
<input id="playerBgColor2" class="hap-ch">
<input id="playerBgColor3" class="hap-ch">

$(".hap-ch").spectrum({
    change: function(color) {
        //how to get playerBgColor id here?
    },
});

I know I can apply spectrum individually like this:

 $("#playerBgColor ").spectrum({
    change: function(color) {

    },
});

But I wanted to know if I can reuse this code for multiple spectrums like in first example.


Solution

  • Well I just played around and yea, you can make use of $(this) within change, which will refer to current input element.

    $(".hap-ch").spectrum({
        change: function(color) {
                alert($(this).attr('id')); //there you get the id
        },
    });
    

    DEMO HERE