views:

221

answers:

2

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....

+2  A: 

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
Ben Rowe
Thanks for you suggestion Ben! I'll have a look at it again a little later and post back to let you know how it worked out for me.
New Developper
Ben, you saved me many gripes! Thanks for your solution, worked like a charm!
New Developper
A: 

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
Mark
Thanks Mark, if I can't get the above solution to work out, I'll look into this a little more to see if I can get my code working. I'll post back to let you know how it works out.
New Developper