2001 posts
  • Elite Author
  • Author had a Free File of the Month
  • Has been a member for 4-5 years
  • Austria
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Microlancer Beta Tester
+3 more
revaxarts says
or replace
jQuery('#portfolioItem').find('li')
with this
jQuery('li', '#portfolioItem')

Should be fast too because the tag selector is also nativ

1383 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
  • Serbia
wpCanyonThemes says

or replace
jQuery('#portfolioItem').find('li')
with this
jQuery('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')
469 posts
  • Author had a File in an Envato Bundle
  • Sold between 250 000 and 1 000 000 dollars
  • Elite Author
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • Has been a member for 4-5 years
  • India
+1 more
SaurabhSharma says

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.

1277 posts
  • Elite Author
  • Has been a member for 5-6 years
  • Sold between 100 000 and 250 000 dollars
  • Author had a Free File of the Month
  • Referred between 200 and 499 users
  • Bought between 10 and 49 items
  • Europe
  • Exclusive Author
+1 more
fuelthemes says

Love this tool: http://css-tricks.com/examples/nth-child-tester/

Great tool. Only (3n) is needed

2001 posts
  • Elite Author
  • Author had a Free File of the Month
  • Has been a member for 4-5 years
  • Austria
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Microlancer Beta Tester
+3 more
revaxarts says

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

606 posts
  • Envato Staff
  • Blog Editor
  • Senior Reviewer
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Author had a File in an Envato Bundle
  • Forum Moderator
  • Microlancer Beta Tester
  • Sold between 10 000 and 50 000 dollars
+9 more
Siddharth Staff says

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.

3365 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred more than 2000 users
  • Has been a member for 4-5 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
+4 more
sevenspark Volunteer moderator says

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

by
by
by
by
by
by