Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to select $(this) AND use selectors in jQuery

I'm wondering if I can use $(this) as well as a class selector before running a function on them.

So rather than doing;

$(this).toggleClass('open');
$('.closed').toggleClass('open');

Do something more like;

$(this, '.closed').toggleClass('open');

Whereas really, the above will select 'this' within the context of '.closed'

Regards,

like image 783
Joel Avatar asked Sep 25 '12 12:09

Joel


1 Answers

You can use add():

$(".closed").add(this).toggleClass("open");

It will add this element to the set of matched elements (i.e. .closed).

like image 112
VisioN Avatar answered Nov 08 '22 20:11

VisioN