how do i select only 3rd( or some other no. of my chioce) li element with jquery? for ex: how do i change the background of only third li with jquery. any help please
Out of curiosity, I fooled around a bit with the selector. As it turns out, using `slice` and act and the whole wrappet set is faster than using `nth-child`. http://www.jsfiddle.net/qbDKN/13/
jAndy
2010-08-09 10:18:50
@jAndy: That's clever work :)
Sarfraz
2010-08-09 10:21:22
@jAndy - I'm getting opposite results, with `eq` almost twice as slow. Remember `nth-child` is a css selector, and is implemented natively by some browsers.
Kobi
2010-08-09 10:28:02
@Kobi: interesting. I tested on Chrome(5) with slightly better results for `slice` (around 300ms).
jAndy
2010-08-09 10:30:12
@jAndy - I get about 9400 for `eq`, and about 5800 for `slice`. But wait - in your code, eq **is** the `nth-child` code. How confusing of you :) . So you are right, it is faster.
Kobi
2010-08-09 10:33:23
@Kobi: Oh dear, my bad I forgot to rename the `time event`. But wow, sounds like a **big** difference than for you.
jAndy
2010-08-09 10:36:44
Also, on the slice code, you don't keep recalling `find` but use the same list, which I suspect skews the results.
Kobi
2010-08-09 10:39:37
@Kobi: well true, but it's still realistic. You can't do it otherwise with the `nth-child(xn)` selector.
jAndy
2010-08-09 10:42:11
@jAndi - one last note - I also found `background` to be slower than `background-color`, which makes sense when you think about it. It probably takes more than a loop to profile the differences `:|`.
Kobi
2010-08-09 10:50:12