Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would go for: </p> <pre><code>$('.someElement', '#myId') </code></pre> <p>Since getElementById is the fastest selector operation, you are first filtering your content and then searching for the class. Also the fewer the selectors the faster the selections will be (depends if using <em>descendant</em> or <em>child</em> selectors - check comments!) . <a href="http://jsperf.com/selector-test-100/2" rel="nofollow">Test</a> based on @Felix test case. </p> <p>Edit: so to enhance this answer. Based on the jQuery <a href="http://api.jquery.com/jQuery/#selector-context" rel="nofollow">documentation</a>: </p> <blockquote> <p>By default, selectors perform their searches within the DOM starting at the <strong>document root</strong>. However, an alternate context can be given for the search by using the optional second parameter to the $() function.</p> </blockquote> <p>So when you provide a second parameter (a context), jQuery will search for that <em>element</em> first and then search within it, so it would be translated from: </p> <pre><code>$('.someElement', '#myId') </code></pre> <p>To: </p> <pre><code>$('#myId').find('.someElement') </code></pre> <p>Now the trick is, trying to find the closest container of your element that actually holds an ID and put it as <em>context</em>, since ID selection is the <strong><em>fastest</em></strong>. Now you would say, why not then using the second already translated one, by I wouldn't actually care that much since the first is cleaner and it's not much slower: </p> <pre><code> $('.someElement', '#myId') $('#myId').find('.someElement') Chrome 8.0.552 36,527 37,400 Firefox 3.6.13 17,632 19,155 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload