Skip to main content

Simple Color Picker - a jQuery color picker control

It's been a while since I build this small plugin for jQuery, While browsing through my files I try to see if this plugin still works with the latest version of jQuery which is 1.3.2 and it did. I have updated the sample and change the file names so it is easy to recognize. I also move the downloads to my google site, Download here.

Focus on the input box below or click on the box next to the control to show color picker.

Basic Sample


jQuery(function($) {
$("#vtrColorPicker").attachColorPicker();
});



Change Background


jQuery(function($) {
$("#vtrColorPicker1").attachColorPicker();
$("#vtrColorPicker1").change(function() {
$("#content-wrapper").css("background-color",$("#vtrColorPicker1").getValue());
});










Comments

  1. This Works for changing a:link
    $("#vtrColorPicker1").change(function() {$('a.myclass').css("background-color",$("#vtrColorPicker1").getValue())});

    this fails on a:hover
    $("#vtrColorPicker1").change(function() {$('a:hover.myclass').css("background-color",$("#vtrColorPicker1").getValue())});

    what am it missing

    ReplyDelete
  2. I am not sure it this selector will work in jQuery.

    a:hover.myclass

    Did you try it outside of color picker change event?

    ReplyDelete
  3. This thing throws errors left and right when I put it in. It gives me this error....

    a.ownerDocument is undefined
    [Break on this error] "border"+this+"Width",true))||0})}a.of...ll;if(a=e.getComputedStyle(a,null))f=

    ReplyDelete
  4. Hi,
    thanks for the nice plugin.
    It works fine but I get an error in firebug when I am using it with jQuery v1.4.2.
    How can I change the default color?
    Thanks again,
    Francesc

    ReplyDelete
  5. This blog is using jQuery v1.4.2 and is working properly. Although I see an error in firebug I think it is not related to color picker.

    BTW you can use setValue function.

    example:
    $("#vtrColorPicker1").setValue(value);

    ReplyDelete
  6. Hey! Thanks for the fast reply.

    Yes, setting default value is easy: Setting value in the input tag or through js in document.ready.
    However, once the color picker is attached, I am not able to change it.
    When I am trying to change the value through an event using the setValue function, the color doesn't change and I am getting 'undefined' in the input box.

    Example:
    $('#reset').click(function () {
    value = '#CC0000';
    $('#vtrColorPicker').setValue(value);
    });

    I can change value in the input with jquery
    $('#vtrColorPicker').val(value);
    but that won't change the color picker.

    Any suggestions?
    On the click event, can I remove the colorpicker an attach a new one with the new value?

    Thanks for your nice work,
    Francesc

    ReplyDelete
  7. Hi again,
    I found a solution to my problem.
    I have been trying different things with setValue and I couldn't make it work.

    To change the color of the colorpicker we can use
    var color = '#CC0000'; $('.ColorPickerDivSample').css({'background-color' : color});

    The class .ColorPickerDivSample is the class for a span where the box with the colorpicker is created, right?

    So, if we want to allow to input manually the color typing it in the input, we could do something like this:
    $('input').change(
    function () {
    var newColor = $(this).val();
    $('.ColorPickerDivSample').css({'background-color' : newColor});
    }
    );

    I am doing all these cos I would like to change the colors through events and cos I have an unknown number colorpickers.
    so the same example for an unknown number of colorpickers could be as follow (although, probably there is a simpler solution):

    //Different ID for all color spans
    $('.ColorPickerDivSample').each( function(i){
    $(this).attr({ id: 'picker' + ++i });
    });

    //Different IDs for the inputs
    $('.vtrColorPicker').each( function(i){
    $(this).attr({ id: ++i });
    });

    $('input').change(
    function () {
    var newColor = $(this).val();
    var inputID = $(this).attr("id");
    var spanID = '#picker' + inputID;
    $(spanID).css({'background-color' : newColor});
    }
    );


    I hope it helps to others.

    Francesc

    ReplyDelete
  8. This is just what I was looking for, but I have run into a problem. It works fine where I don't use other JQUery scripts, but I have script libraray that contains a document ready handler which seems to prevent the handle in your script from creating your object instance. Any suggestions?

    ReplyDelete

Post a Comment

Popular posts from this blog

dnnAlert and dnnConfirm jQuery Plugin

I like the idea of using jQuery Plugins in DNN for interactive and consistent user interface which is why I digg into using dnnAlert and dnnConfirm plugins, however after a quick look on the documentation I notice the following. For dnnAlert you can't set the title. We are allowed to set the OK button text using okText option but not the dialog title? For dnnConfirm. This is very usefull when you just want a confirmation dialog upon click on your button or link however you can't use it like javascript confirm function or at least I don't know how to use it like below: if(confirm('Question?')){//some code}

A javascript library for formatting and manipulating numbers.

Numeral.js , while less complex, was inspired by and heavily borrowed from  Moment.js  so if you have use momentjs before you should be familiar with this library. Numbers can be formatted to look like currency, percentages and more. How to use: numeral(1000).format('0,0') returns 1,000 numeral(1230974).format('0.0a') returns 1.2m numeral(1000.234).format('$0,0.00') returns $1,000.23 numeral(1230974).format('($ 0.00 a)') returns $ 1.23 m numeral(1).format('0%') returns 100% numeral(0.974878234).format('0.000%') returns 97.488% For more sample and options go to  http://numeraljs.com/ .