Yep, the nth child selector is the way to go. But keep in mind that this take a performance hit if the number of portfolio items is huge. [Actually huge.]
In those cases, you’re better of checking the position of an entry and assigning an appropriate class through server side code, PHP in this case, I’m assuming. Then you can merely use the class selector to manipulate specific elements in the front end, as needed.
This way, computation is offloaded to the server instead of the client having to do the job. Again, this only applies if you have a huge number of elements to parse. Otherwise, the simpler method noted above should do.
In those cases, you’re better of checking the position of an entry and assigning an appropriate class through server side code, PHP in this case, I’m assuming. Then you can merely use the class selector to manipulate specific elements in the front end, as needed.
+1. I always use an algorithm in php to detect nth element for my portfolio pages. This makes sure that the page still looks fine with non-availability of javascript. But it gets into trouble when implementing with sortable portfolio.
His code will find the element with the ID and then filter for the tag. Since both use native JavaScript methods, it’ll be blazingly fast.
Your method, while quite adequate for general use, is inefficient. Sizzle, jQuery’s selector engine, handles selectors right to left so there’s a lot of overhead involved. It finds all li elements and then sees whether the ID is a parent.
Mostly, you don’t have to worry about these issues since performance becomes an issue only if you have too many elements.
For anyone who is interested in/confused about these types of efficiency questions, this quick article from Jeffrey Way is a useful read: Quick Tip: Think Right-to-Left with jQuery