0

Following is the the snippet of a widget:

(function($){
    var ele,_this;
    $.widget('test.mywidget',{
       _loadComments:function(d)
       {
           $.each(d,function(k,v){
            var p=$('<div/>').addClass('comment').attr('id',v.id);           
            p.append("<textarea class='ui-border' onkeypress='_this._submitReply()'></textarea>");
            ele.find('.prevComments').append(p);
           });
       },
       _submitReply:function(){}
       _attachEvent:function()
       {
           ele.on('change','textarea',function(){
              alert('ta pressed');
           });
       }
   _create:function(){
       ele=this.element;
       _this=this;         
       this._loadComments(somedata);          
       this._attachEvent();
   }
});
});

The problem is if this._loadComments(somedata); has two record and then if i press key inside textarea alert('ta pressed'); get called 2 times, if it is 10 data it is called 10 times. It should not happens like this. It should call alert message only once for individual textarea.

How can i attach event to individual element? I think here I am attaching event to all textarea that's why.

If i add inline event like onkeypress='_this._somefunction()' it says _this is undefined but it is there.

4
  • try jsfiddle.net/arunpjohny/y7PbH/1 Commented Nov 24, 2013 at 11:32
  • i m not seeing any changes here. Is it just you have pasted my code Commented Nov 24, 2013 at 11:48
  • no.. I used this.element.on('change' Commented Nov 24, 2013 at 11:59
  • here ele is the same thing Commented Nov 24, 2013 at 12:03

1 Answer 1

0

one option is to remove the event handler each time before you add it.

function handleChange(){
  alert('ta pressed');
}


_attachEvent:function()
   {
       ele.off('change','textarea',handleChange);
       ele.on('change','textarea',handleChange);
   }
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.