Wrong terminology. What you are asking has nothing to do with caching.
And you don't cache "APIs". You cache jQuery objects.
Not
cached:
$(".some-selector').hide();
.
.
.
$('.some-selector').show();
.
.
.
$('.some-selector').addClass('foo');
Cached:
var
$someElements = $('.some-selector');
$someElements.hide();
.
.
.
$someElements.show();
.
.
.
$someElements.addClass('foo');
When we talk about
"caching" jQuery objects, we mean saving them in
variables so that they can be re-used later. It avoids the
overhead of having to construct new identical jQuery objects
over and over.
As to your
original problem, as Jake illustrates, jQuery functions that
DO or CHANGE something will apply to multiple elements, if
the selector used selects multiple elements. You do not have
to repeat yourself or write a loop. jQuery does it for you.
This is a great convenience.
With your
original markup, this would work:
$('#id1,#id2,#id3,#id4,#id5').hide();
Jake's solution is
better. IDs are evil.
Use a
single CSS class that you apply to multiple elements.
IDs with
appended numbers are especially evil, and almost always indicate
that the code could be better refactored using a common CSS class
instead. In the
Ruby world, we'd say this "raises a smell". It's
an indication of poorly-structured code that jumps right out at
you and so bears investigation and re-thinking.
Note that jQuery functions
that FETCH some information, though, return only a single value.
It is the value associated with the FIRST matched
element.
var
html = $('#id1,#id2,#id3,#id4,#id5').html();
will return the HTML from
the element with ID id1 . If you wanted to fetch
all of them, then you'd have to write a loop.