Having the following html snippet
<div class="something"> <p>Some text</p> </div> <div class="somethingElse"> <p>some other text</p> </div>
I think the following jquery snippets are identical (will have the same result):
$(".something").find("p").css("border", "1px solid red"); $("p", ".something").css("border", "1px solid red");
My question is, whether one snippet is better than the other and should be used
The calls are not identical.
According Brandon Aaron, who apparently worked on jQuery, and also according to the live tests here, the find method is always faster. See results in the screenshot below. Please comment if I am missing something.
With a 10% or greater difference in speed, depending on browser, it definitely seems worth using find.
Further explanation at Brandon's site is here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With