Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$(this) is not working

I am using ColorPicker Plugin. I initialized the plugin with following code :

$(".colorpic").ColorPicker({
    color: '#0000ff',
    onShow: function (colpkr) {
        $(colpkr).fadeIn(500);
        return false;
    },
    onHide: function (colpkr) {
        $(colpkr).fadeOut(500);
        return false;
    },
    onChange: function (hsb, hex, rgb) {
        $(this).css('backgroundColor', '#' + hex);  <= $(this) not working 
    }
});

Now my problem is that $(this) is not working in onchange event. Help me out please?

like image 225
King Kong Avatar asked Jun 19 '12 13:06

King Kong


People also ask

What does $( this mean in jQuery?

$(this) is a jQuery wrapper around that element that enables usage of jQuery methods. jQuery calls the callback using apply() to bind this . Calling jQuery a second time (which is a mistake) on the result of $(this) returns an new jQuery object based on the same selector as the first one.

Why click is not working?

To turn it on, go to Start > Settings > Devices > Mouse > Related Settings > Additional Mouse Options. The Mouse Properties window will pop up. At the bottom of the Buttons tab, you'll see the ClickLock options. Put a tick in the checkbox to enable it.

Why on click is not working jQuery?

So Why Does It Happen? JQuery OnClick Method is bound to an element or selector on page ready/load. Therefore if that element you want to click isn't there at the time of page ready, the binding can't happen.


1 Answers

Try like this:

$(".colorpic").each(function(){
    var $this = $(this);

    $this.ColorPicker({
        color: '#0000ff',
        onShow: function (colpkr) {
            $(colpkr).fadeIn(500);
            return false;
        },
        onHide: function (colpkr) {
            $(colpkr).fadeOut(500);
            return false;
        },
        onChange: function (hsb, hex, rgb) {
            $this.css('backgroundColor', '#' + hex);
        }
    });
});
like image 100
Mariusz Jamro Avatar answered Oct 05 '22 19:10

Mariusz Jamro