How can I write this jQuery snippet more beautiful

$("#rightCol").children().children().children("div.entry").length

I tried this

$("#rightCol").children().eq(1).children("div.entry").length

or

$("#rightCol").children(":eq(1)").children("div.entry").length

without success. ideas?

+3
source share
4 answers

Depending on the layout (do you have a DIV with this class at other levels that you want to avoid?), You can go through with

$('#rightCol').find('div.entry').length
+4
source

You can do this using a child selector ( >) , for example:

$("#rightCol > * > * > div.entry").length

Although, if you know the type of the child, I would use it over *. If the level does not matter, only the descendant selector ( ) will work .

$("#rightCol div.entry").length
+3
source

, ?

$("#rightCol *:eq(1) div.entry").length
+3

, .

$("#rightCol > * > * > div.entry")

. "div.entry" "#rightCol", .

$("#rightCol div.entry")
+3

Source: https://habr.com/ru/post/1770766/


All Articles