Joseph Khella Joseph Khella - 3 months ago 5
jQuery Question

Get the element calling a function in JQuery?

I have two JQuery elements like

and I defined a proto for it called
Implementation, How can I know that
is the one who called it if I did
Thank you,


You use this:

  • Within your plugin code, it will refer to the jQuery object your plugin was called on
  • Within event handlers and such, it refers to the DOM element

Quick example:

// Our "nifty" plugin
(function($) {
  // Some default options
  var niftyDefaults = {
    color: "green"
  $.fn.nifty = function(options) {
    // Get the options to use
    options = $.extend({}, niftyDefaults, options);
    // Here, `this` is a jQuery object; usually
    // you loop through it:
    this.each(function() {
      // Let's have our "nifty" set up an event handler
      // Note that here, `this` is the DOM element
      // for this iteration of the `each` loop
      $(this).on("click", function() {
        // And respond to it by setting the color
        // Here, `this` is the DOM element
        $(this).css("color", options.color);

// Using it
$(".foo").nifty({color: "blue"});
$(".bar").nifty(/*defaults to green*/);
<div class="foo">foo</div>
<div class="bar">bar</div>
<script src=""></script>