0

I usually define all my jQuery variables at the top of the module I am working on. There sometimes becomes a case when I would like to combine the variables to effect them both. So for example, imagine a case where I have defined them like so:

$myVar = $('#my-var');
$myOtherVar = $('my-other-var');

If I want to remove them both from the DOM, I would have to do the following:

$myVar.remove();
$myOtherVar.remove();

Is there a way I could create another variable and combine them? So I wouldn't have to do this:

$('#my-var, #my-other-var).remove();

Thanks.

3
  • use add() like $myOtherVar.add($myVar).remove() Commented Oct 13, 2014 at 17:13
  • I think he means he wants to append all the elements he wants to remove in one statement and then perform the 'remove-all-at-once' to reduce operations on the DOM... I don't really know since im not a huge Jquery guy, but something array based sounds like your best bet. Commented Oct 13, 2014 at 17:17
  • Also i hope you're not polluting the global scope and using IIFY's with your modules :P Commented Oct 13, 2014 at 17:18

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.