Question

How do I pass $(this) object to the function inside click event.

$('#mainwrap img').click(function(){      
    mcImageManager.browse({
        oninsert : function(o) {
            src= o.focusedFile.url;
            $(this).attr("src", src );
        }
    });
});

Any suggestion will be greatly appreciated.

Was it helpful?

Solution

this is a special variable - each time a function is entered it takes on a new value.

In this case to make the outer function's this available in the inner function, you can bind a new variable to it outside of the nested function:

$('#mainwrap img').click(function() {
    var $this = $(this);      // new variable here 
    mcImageManager.browse({
        oninsert : function(o) {
            src= o.focusedFile.url;
            $this.attr("src", src );  // referenced here
        }
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top