X

LATEST #JQUERY NEWS

Using the .find() Method to Optimize Selectors


Sometimes trying to use an extra complicated selector with jQuery can slow your code down a bit. If you’re trying to use a selector with multiple classes and IDs, for example, things can get a little slow. A nice jQuery trick to avoid this slowing down of your code is to employ the .find() method to help your jQuery more efficiently understand exactly what it’s looking for. Instead of using a long-winded selector like, for example:

$(‘#myDiv h2.green’).slideUp()

You can use the .find() method to speed up the process of selecting that h2 tag with the class “green”:

$(‘#myDiv’).find(‘h2.green’).slideUp()

It might not seem like it makes a huge difference, but it inserting this .find() method into your jQuery code can definitely help to make your code more lightweight and faster — but be careful when using it, because it’s not supported on some of the older browsers.

Recently Published

article image
»

Changing images in a loop on hover using jQuery

Why use this Function? Changing images in a loop on mouse over is a ...

article image
»

How to Dynamically Trim Text Using jQuery

This easy beginners jQuery tutorial demonstrates how you can use ...

article image
»

jQuery Snippets: Getting the Current Year

Here’s a fun, simple, and incredibly useful jQuery that you can ...

article image
»

jQuery Snippets: How to Conditionally Enable Submit

One easy way to prevent users from submitting an empty form in a ...

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...

article image
»

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of┬ácheckbox input fields ...

article image
»

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image
»

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image
»

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...