jQuery plugin: Colorpicker
Question
Hey all, I'm using this colorpicker (http://www.eyecon.ro/colorpicker) and am trying to capture the hex value so that I can use it on the server side to store the selected color. If you check out the link provided, I'm using the last option:
$('#colorSelector').ColorPicker({
color: '#0000ff',
onShow: function (colpkr) {
$(colpkr).fadeIn(500);
return false;
},
onHide: function (colpkr) {
$(colpkr).fadeOut(500);
return false;
},
onChange: function (hsb, hex, rgb) {
$('#colorSelector div').css('backgroundColor', '#' + hex);
}
});
My problem is that I can't seem to get the hex value from it...I've tried simply calling the name of the input to get its value, but it won't work (when you click away to make the colorpicker disappear, the input changes to 'style="display:none;"' so I can't get anything from it. Then, I tried pulling the value using some simple jQuery calls, but got nothing...
Please help....
Solution
Firstly welcome to Stack Overflow!
The onChange event should give you your answer.
// initial colour value
var currentHex = '#0000ff';
$('#colorSelector').ColorPicker({
color: currentHex,
onShow: function (colpkr) {
$(colpkr).fadeIn(500);
return false;
},
onHide: function (colpkr) {
$(colpkr).fadeOut(500);
return false;
},
onChange: function (hsb, hex, rgb) {
// every time a new colour is selected, this function is called
alert(hex); // left for debugging
currentHex = hex;
}
});
// The currentHex value should easily be accessible as a vaiable
OTHER TIPS
Looking at the source the author uses this to get it:
var div = $('#colorSelector'); // DOM element it is attaced to
var color = $('#' + $(div).data('colorpickerId')).data('colorpicker').color // get id of colorpicker, the object it stored in data and get color