Reputation: 1405
If you are caching elements with javascript, which is more efficient?
HTML
<div id="parent">
<div id="child"></div>
</div>
Javascript:
var parent = $('#parent');
var child = $('#child');
or
var parent = $('#parent');
var child = $(parent).find('#child');
Is one better than the other? Or better practise? I'm writing a lot of code like this, and I am currently using find() to get specific elements of parents that are already cached.
Thanks
Upvotes: 1
Views: 55
Reputation: 19368
As @PSL says, as ids, the first is better, but with classes, the second would be faster because then you don't have to search the whole document for .child
you just have to search within the #parent
Also, you can go:
var child = parent.find('#child');
instead of
var child = $(parent).find('#child');
Upvotes: 1