Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andrew Dickens
18,352 PointsTree Traversal - what is so great about .find()?
Hiya, working my way through Tree Traversal and there's times where I've thought, is this entirely necessary I could just use any regular selectors, a good example is .find()
so this
$( "p" ).find( "span" ).css( "color", "red" );
could be written like this which seems sort of easier/compact
$( "p span" ).css( "color", "red" );
So could someone tell me what the advantages of traversing are? thanks
2 Answers
Steven Parker
243,656 Points
jQuery .find() can be very handy.
You picked an example where it wasn't really needed.
But I use it all the time, often in event handlers where you need to find elements relative to the event target or something associated with it. Consider this example:
$(this).closest(".control-group").find(".indicator").css("color", "red");
Try doing that with a simple selector.
P.S. Did you realize your profile photo is sideways?
Andrew Dickens
18,352 PointsThanks Steven, yes my photo is sideways, so i edited it 90deg and uploaded it again and it just turned the other way so i gave up...