0

It returns undefined for var "el" the way I have this code

$('.element').each(function(){
    var el = $(this);
    setTimeout(function(el) {
        el.css({ marginTop: -100 });
    }, 550);
    $(window).resize(function(el) {
        setTimeout(function(el) {
            el.css({ marginTop: -100 }); //this is where el is undefined
        }, 550);            
    })
});

Any idea what am I doing wrong?

1
  • Can you provide a fiddle? Commented Oct 3, 2013 at 13:44

3 Answers 3

3

I think it's because of el as function arguments. Try to remove them.

$('.element').each(function(){
    var el = $(this);
    setTimeout(function() {
        el.css({ marginTop: -100 });
    }, 550);
    $(window).resize(function() {
        setTimeout(function() {
            el.css({ marginTop: -100 }); //this is where el is undefined
        }, 550);            
    })
});

And don't forget to crucify me if I'm wrong :P

EDIT: Just to explain what's going on, the function in resize is passed an event object, so it overwrites the el variable you have set. Event isn't a jQuery object, so you cant call css function on it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man, issue solved, now I got the point, but the function is not doing what I need, have to dig into it.
0

The el parameters that you're passing into the setTimeout and the resize event are unnecessary,

working example: http://jsfiddle.net/atg7F/

Comments

0

You're overwriting the el. Try this:

http://jsfiddle.net/Um7Tp/1/

$('.element').each(function(){
    var el = $(this); // this var was overwritten...
    setTimeout(function() { // here
        console.log(el);
        el.css({ marginTop: -100 });
    }, 550);

    $(window).resize(function() { // here
        setTimeout(function() { // and here
            el.css({ marginTop: -100 }); 
            console.log(el);
        }, 550);            
    })
});

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.