tylik
tylik

Reputation: 1078

Jquery access to $(this) inside a plugin

Is there any way to get an access to $(this) when the plugin is initialized? for example I have a plugin resizable where I need access to an element that is being resized, so I could use it later in a method that is declared inside a plugin

$('.someClass').resizable({  
  resize:function(event, ui) {
   $(this).doSomeStuff() //how to get access to $(this)? Where it should be declared?
  }
})

I've figured out the solution:

$('.someClass').each(function(){ 
  var me = $(this); 
  $(me).resizable({     
    resize: function(event, ui) { 
          $(me).doSomeStuff();
        }
   });
});

All that it took just to make sure the plugin is inside of $('').each() method

Upvotes: 1

Views: 155

Answers (2)

Dawson Toth
Dawson Toth

Reputation: 5680

$("textarea").resizable({
    stop: function (evt, ui) {
        $(this).find(':input').focus();
    }
});

Upvotes: 1

zack
zack

Reputation: 3198

its a problem of scope, so cache the element in a variable:

var elem = $('.someClass');
elem.resizable({  
  resize:function(event, ui) {
   elem.doSomeStuff() //how to get access to $(this)? Where it should be declared?
  }
});

edit

n.b. logging 'event', 'ui' to the console should clarify what those args provide you with, also check the api docs: http://docs.jquery.com/UI/Resizable

$('.someClass').resizable({  
  resize:function(event, ui) {
   console.log(event);
   console.log(ui);
  }
});

Upvotes: 1

Related Questions