javascript - Dynamically create js function -


i want dynamically (in loop) bind function .click() event of several divs. click function should hide clicked div. way trying it, lost reference div, , "this." not working me too.

here function want bind:

function do_hide() {       is_anim = true;       $(this).animate({          opacity: 0.25,         height: 'toggle',         width: 'toggle'     }, 5000, function() {          is_anim = false;         this.hide();     }); } 

thx help.

edit: solution of ghayes

do_hide() called here:

for (var = 0; < n; i++) {     p[i] = $("#btn"+(i+1));  p[i].click(function() {    do_hide.call(this);  }); } 

you can bind scope 'do_hide' when call it. suggest pattern following: (working jsfiddle)

 $('.stackoverflow').click(function() {    do_hide.call(this);   });   var do_hide = function()  {      is_anim = true;      $(this).animate({      opacity: 0.25,      height: 'toggle',      width: 'toggle'    }, 5000,                    function() {                     is_anim = false;                     this.hide();                   });  }; 

hope helps!


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -