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.



Responsive Menu
Add more content here...