Allen S
Allen S

Reputation: 3539

jQuery: Target $(this) and an element

On a click I'm removing two elements $(this) and $("#foo").

Currently my code looks like this:

$(this).remove();
$("#foo").remove();

How can I optimize this without repeating myself? I've tried:

$(this, "#foo").remove();

and

$(this "#foo").remove();

and

$(this && "#foo").remove();

But it doesn't seem to work...

Upvotes: 1

Views: 56

Answers (1)

Blender
Blender

Reputation: 298206

You can use .add() to add the current element to the set of matched elements:

$('#foo').add(this).remove();

I would use the verbose way instead. It's more readable.

Upvotes: 5

Related Questions