Mamen
Mamen

Reputation: 1436

Uncaught TypeError: Cannot use 'in' operator to search for 'height' in undefined

i have got following code :

$('.p1').click(function(){
            var num = 10;
            var count = 0;
            var intv = setInterval(anim,800); 
            function anim(){
                count++;
                num--;
                if(num==0){clearInterval(intv);}
                $(this).hide(function(){
                    $(this).appendTo('#box_'+count);
                }).delay(500).fadeIn();

            }
            anim();
        });

i don't know ,if the animation put in function the error appears, please help me

Upvotes: 2

Views: 2448

Answers (1)

MrCode
MrCode

Reputation: 64526

The anim() function has no this context. The click event does have it, but that isn't automatically passed on to your other function. You can set it using .bind() and .call():

var intv = setInterval(anim.bind(this),800);
...
...
anim.call(this);

.bind() is used on the setInterval line because you want to pass the function whilst having the this context set (without calling it). .call() is used on the last line because you want to call the function and set the this context.


Alternatively you can store the this context as another variable, making it accessible in the anim() function:

// in the click event
var element = this;
function anim(){
  count++;
  num--;
  if(num==0){clearInterval(intv);}
    $(element).hide(function(){
    $(element).appendTo('#box_'+count);
  }).delay(500).fadeIn();

}

Upvotes: 2

Related Questions