0

I am unable to access parent object with in the event handler of the object!

I am creating a widget:

(function( $ ) {

  $.widget('cs.window',{

     _create:function(){
       var me = this;
       this.append('<div class="close">X</div>');
       var close = this.element.find('.close');
       close.bind('click',function(){
        alert($(this));// this is referring event handler here
                       //but how to access `me` object here
        me.element.hide();//I want to achieve this, about but its saying me is undefined
      });
     }
  });

}(jQuery));

How to use me object in the click event handler?

5
  • If you look at jquery click you'll see some great examples: api.jquery.com/click Commented Apr 16, 2014 at 12:39
  • @PaulS. I tried but saying me is undefined. Commented Apr 16, 2014 at 12:42
  • 1
    @RamaRaoM log the value of this and of me when inside _create and see what you get. It may be that this is undefined there (can happen depending on how the function is ultimately invoked) Commented Apr 16, 2014 at 12:44
  • @RamaRaoM: me should not be undefined in the above, not if this wasn't undefined when _create was called (which probably wasn't the case, or you'd've seen a different error, during _create). Commented Apr 16, 2014 at 12:44
  • @PaulS. No,this is not undefined out of the event.Its referring current widget object. Commented Apr 16, 2014 at 12:46

1 Answer 1

1

If you bind a function with this._on then the this inside the callback is the widget itself. Try:

this._on(close, {
    click: function(){ this.element.hide(); }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.