When using jQuery for DOM traversal both of these return the same results (I believe):
$("whatever").find(".foo .bar")
$("whatever").children(".foo").children(".bar")
Which is preferable to use?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
They are not equivalent, as I’ll explain below, but if adjust them to match,
.children()for speed,.find()for brevity (extra work inside Sizzle, parsing this for starters), you decide which is more important.The first has different results, but if you know they’re children, you can do this:
This would be equivalent to your second function. Currently, the first as you have it would find this:
The second would not, it requires that
.foobe a direct child ofwhateverand.barbe a direct child of that, the.find(".foo .bar")allows them to be any level deep, as long as.barin a descendant of.foo.