- Microlancer Beta Tester
- Author had a Free File of the Month
- Has been a member for 3-4 years
- Item was Featured
- Author was Featured
- Austria
- Exclusive Author
- Referred between 200 and 499 users
jQuery('#portfolioItem').find('li')
with this
jQuery('li', '#portfolioItem')
Should be fast too because the tag selector is also nativ
revaxarts said
or replacejQuery('#portfolioItem').find('li')with thisjQuery('li', '#portfolioItem')Should be fast too because the tag selector is also nativ
Aren’t you maybe targeting all list items and the element with the id of portfolioItem. 
To avoid the find use
$('#portfolioItem li')
- Author had a File in an Envato Bundle
- Sold between 250 000 and 1 000 000 dollars
- Author was Featured
- Item was Featured
- Contributed a Tutorial to a Tuts+ Site
- Exclusive Author
- Has been a member for 3-4 years
- India
Siddharth said
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.
- Author was Featured
- Has been a member for 4-5 years
- Author had a Free File of the Month
- Sold between 100 000 and 250 000 dollars
- Bought between 10 and 49 items
- Europe
- Exclusive Author
- Referred between 200 and 499 users
themolitor said
Love this tool: http://css-tricks.com/examples/nth-child-tester/
Great tool. Only (3n) is needed
- Microlancer Beta Tester
- Author had a Free File of the Month
- Has been a member for 3-4 years
- Item was Featured
- Author was Featured
- Austria
- Exclusive Author
- Referred between 200 and 499 users
wpCanyonThemes said
Aren’t you maybe targeting all list items and the element with the id of portfolioItem.![]()
To avoid the find use
$('#portfolioItem li')
No, I’m targeting al list items within the portfolioItem
jQuery('li', '#portfolioItem') != jQuery('li, #portfolioItem')
Furthermore jQuery don’t has to check the whole DOM . It just start with the #portfolioItem
- Microlancer Beta Tester
- Envato Staff
- Author had a File in an Envato Bundle
- Lead Reviewer
- Blog Editor
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Forum Moderator
- Sold between 10 000 and 50 000 dollars
- Author was Featured
wpCanyonThemes said
To avoid the find use$('#portfolioItem li')
No, Xaver’s method is much better.
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.
- Sold between 250 000 and 1 000 000 dollars
- Community Moderator
- Author was Featured
- Item was Featured
- Bought between 50 and 99 items
- Referred between 1000 and 1999 users
- Has been a member for 3-4 years
- Repeatedly Helped protect Envato Marketplaces against copyright violations
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
