jQuery multiple selector with $(this)

Lukáš Řádek picture Lukáš Řádek · Jul 6, 2013 · Viewed 7.7k times · Source

is there any way to make this work:

$(this, '#foo')

with that I want to select "this" element and #bar as well. For selecting let's say two IDs, it is just as easy as '#foo, #bar', but when I want one of those two to be "this" I cannot get it to work.

Answer

krishwader picture krishwader · Jul 6, 2013

Problem with your approach

$(this, '#foo')

The above line will search for this inside an element with id set to foo, which isn't what you want here.


Solution

You can use add() for this :

var $el = $(this).add('#foo')

Now $el will contain $(this) & $("#foo") on which you can perform operations on.


More info on the methods used

add("selector")