Search code examples
javascriptjqueryhideobjectinstantiation

Hide cached objects in jQuery


I'm not sure if "cached" is a correct term for this one. Maybe I should use "instantiated" instead. However, say I want to "cache" several objects, to save some resources:

var $foo = $("#foo"),
$bar = $("#bar");

Now, if I want to hide them, can I use a one-liner instead of:

$foo.hide();
$bar.hide();

I reckon that this one is quite simple (read: "stupid"), but hey... I can't figure it out all by myself...


Solution

  • You can also use .add() to roll up a bunch of jQuery objects and selectors:

    $foo.add( $bar ).add('.someclass').add( $other_objects_or_selectors ).hide();