Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a more efficient way to write $('parent > child')?

Given the following selector $('parent > child'), I believe jQuery will first query for all 'child' elements before filtering down to those that are direct descendants of 'parent'. This can be very inefficient.

My first instinct is to use $('parent').find('child'), but the result is obviously not the same as $('parent > child').

Is there a better way to write this selector?

like image 459
Mark Brown Avatar asked Oct 26 '11 00:10

Mark Brown


1 Answers

jQuery has a .children() method that only selects the immediate children.

Also, don't worry about this stuff! Unless you have a gigantic app or it's out of curiosity there's no reason to delve into this. If you are using a selector way too often just "cache" it: $tabs = $('.tabs'). A good practice is to use $ in front of variables that represent selectors.

like image 96
CamelCamelCamel Avatar answered Sep 25 '22 05:09

CamelCamelCamel