[jQuery] getting the first child
I was trying to find the best way to get the first child element in
jQuery. Seems like these all work...
$('#parent').children(":first")
$('#parent').children(":first-child")
$( $('#parent').children()[0] )
$('#parent').find(":first")
$('#parent').find(":nth-child(1)")
There are all a bit ugly. I was hoping for a child() method or
something similar, since jQuery has a 'firstChild' element internally.
I profiled these methods in firebug, to help me to decide. Odd
results.
(called 100x)
1. 887ms // children(":first")
2. 888ms // children(":first-child")
3. 593ms // children()[0]
4. 673ms // find(":first")
5. 498ms // find(":nth-child(1)")
I would never have guessed that find(":nth-child(1)") would be the
fastest. Maybe my profiling setup is borked, but i did it a few times
in various orders and achieved similar results.
So, is there a cleaner way of getting the first child? Is there a
faster way?
-jason