Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reference the outer "$(this)" in jquery?

Tags:

jquery

this

Let's say I have code like this:

$('.myClass').each(function(){
    $('#' + $(this).attr('id') + "_Suffix").livequery('click', function(){
        doSomething($(this));
    });
});

The $(this) that I pass to the doSomething function is what's in the second jquery parenthesis - $('#' + $(this).attr('id') + "_Suffix"). How do I reference what's in the first parenthesis - what the original this referred to? ( $('.myClass').each )

I assume I could save it into a variable, and then use that variable:

$('.myClass').each(function(){
    outerThis = $(this);
    $('#' + $(this).attr('id') + "_Suffix").livequery('click', function(){
        doSomething($(outerThis));
    });
});

But is there any way to reference it without doing this?

like image 343
froadie Avatar asked Oct 21 '25 10:10

froadie


1 Answers

You need to put it in a separate variable:

$('.myClass').each(function(){
    var outer = $(this);
    $('#' + $(this).attr('id') + "_Suffix").livequery('click', function(){
        doSomething(outer);
    });
});

Also, livequery is deprecated; you should use live instead.

like image 50
SLaks Avatar answered Oct 25 '25 06:10

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!