M
M
magicstream2011-08-22 21:09:39
JavaScript
magicstream, 2011-08-22 21:09:39

jQuery parents().get(2) vs parent().parent().parent()?

Help me figure out which method is better and faster

Answer the question

In order to leave comments, you need to log in

5 answer(s)
Y
Yngvie, 2011-08-22
@Yngvie

As for the speed, I won’t say, purely visually I would choose the first option out of the two proposed.
And ideally, I would use a selector in the parents call
el.parents('.container')
el.parents('li')

M
magicstream, 2011-08-22
@magicstream

the question concerns only the aesthetics of the code and speed.
is it correct to repeat parent() 3 times or is it better to get the result with a single call to parents().
the question can be reformulated for the 5th parent case:
parents().get(5) vs. parent().parent().parent().parent().parent()

M
magicstream, 2011-08-23
@magicstream

Actually, the question itself appeared when analyzing the code of the Indians, a variant with the repetition of parent () is very common. And moreover, they can repeat more than 10 times.
At first I condemned this approach. But apparently their approach has the right to life.

F
Frezer, 2019-08-20
@frazerto

.closest();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question