eph baum dot dev

← Back to blog

(How I) Use Multiple Cached Selectors with jQuery

Published on 03/11/2014 05:25 PM by Eph Baum

I am embarrassed to admit how often it really happens, but I frequently forget how to use multiple cached selectors with jQuery and usually just fire a quick Google search and then kick myself when I see a Stack Overflow post like this one and immediately remember how I do this.

The Two Methods

I am referring to is trying to select more than one element like this:

$( '#main_content, #sub_header' )

If you know what you’re doing this should return an array with two DOM elements inside.

When using cached selectors, however, this doesn’t work the same way:

var $main_content = $( '#main_content' ), $sub_header = $( '#sub_header' );

There are two ways that you can handled this. One of them is using the .add() method like this:

$main_content.add( $sub_header )

The problem here is that this is method is know to be pretty slow, so what’s a better solution (even if speed doesn’t really matter)?

As you might have seen from the link I referenced above, this is what is often recommended:

var $main_content = $( '#main_content' )[0], 
    $sub_header = $( '#sub_header' )[0]; 

$( [ $main_content, $sub_header ] );

This works pretty well, but I feel like there are some pitfalls here worth keeping in mind.

Pitfalls?

I don’t know if the term pitfalls is particularly accurate. Perhaps they’re more potential ‘gotchas’ that I worry about. First, this recommend method relies on knowing for certain that you’ll only ever get one element to match in your cached selector. If you’re matching multiple elements you’re simply excluding all but the first when actually defiing the variable.

The other concern I have depends on how else you may intend to use these selectors. If you want to use this selector on its own for chaining, for example, this notation will at best be an annoyance to readability and semantics and at worst may behave in a way you wouldn’t expect.

This:

$( '#main_content' )[0].removeAttr( 'style' )

may not behave identically to this:

$( '#main_content' ).removeAttr( 'style' );

Actually, it probably will, but I am meaning more complicated chains and methods and scoping that could become a nightmare.

What do I do?

Well, I’m glad you asked me that because that’s the exact reason why I am writing this post!

What I do is a little different but I think makes a great deal more sense to me. Perhaps I’m an idiot and would love to hear how.

Until I do, however, I’ll do this:

var $main_content = $( '#main_content' ),
    $sub_header = $( '#sub_header' ); 

$main_content.removeAttr( 'style' ); 

$( [ $main_content[0], $sub_header[0] ] ).fadeIn( 'fast' );

Did you see that? What I did? Yeah, it’s minor, but makes an enormous amount of sense in the end.

That’s it for now. See you next time!

Written by Eph Baum

  • AI

    AI

    This may read as alarmist. It might not be alarmist enough. On AI as sophisticated autocomplete, not thinking—and why that gap matters to the future.

  • Slow Down

    Slow Down

    Speed worship is anxiety wearing a productivity costume. The case for slowing down—and why going fast is often how you screw everything up.

  • Making Brutalist Design Accessible: A Journey in WCAG AA Compliance

    Making Brutalist Design Accessible: A Journey in WCAG AA Compliance

    How I transformed my brutalist blog theme to meet WCAG AA accessibility standards while preserving its vibrant, random aesthetic. Talking about contrast ratios, color theory, and inclusive design.

  • Building Horror Movie Season: A Journey in AI-Augmented Development

    Building Horror Movie Season: A Journey in AI-Augmented Development

    How I built a production web app primarily through 'vibe coding' with Claude, and what it taught me about the future of software development. A deep dive into AI-augmented development, the Horror Movie Season app, and reflections on the evolving role of engineers in the age of LLMs.

  • Chaos Engineering: Building Resiliency in Ourselves and Our Systems

    Chaos Engineering: Building Resiliency in Ourselves and Our Systems

    Chaos Engineering isn't just about breaking systems — it's about building resilient teams, processes, and cultures. Learn how deliberate practice strengthens both technical and human architecture, and discover "Eph's Law": If a single engineer can bring down production, the failure isn't theirs — it's the process.

  • Using LLMs to Audit and Clean Up Your Codebase: A Real-World Example

    Using LLMs to Audit and Clean Up Your Codebase: A Real-World Example

    How I used an LLM to systematically audit and remove 228 unused image files from my legacy dev blog repository, saving hours of manual work and demonstrating the practical value of AI-assisted development.