0

What the better way to select an element inside another:

  1. var elem = $('#container').find('ul');
  2. var elem = $('#container ul');
  3. var elem = $('ul', '#container');

Any thoughts?

Filipe
  • 1,189
  • 4
  • 15
  • 30
  • Doesn't really matter, they all work and the speed difference is so small you wouldn't even notice it on thousands of elements. – adeneo Mar 07 '14 at 20:17
  • That said, the second one is usually what you want, unless there's a reason to use `find()`, and the third is just a shortcut for the first. – adeneo Mar 07 '14 at 20:18
  • Because #1 and #3 are functionally equivalent, you can find your answer over at [jQuery: $('#id tag') vs. $('#id').find('tag') - which is preferable?](http://stackoverflow.com/questions/11502563/jquery-id-tag-vs-id-findtag-which-is-preferable) – Impirator Mar 07 '14 at 20:23
  • there is one more way....use parent.children(childElement) method to select an element inside another – Arjit Mar 07 '14 at 20:28

2 Answers2

1

Here's a test with Chrome 33, Firefox 14 and IE 11

Code use:

<div id="container"> 
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
</div>

enter image description here

Juan
  • 4,910
  • 3
  • 37
  • 46
0

The answer use to be The shorter the better. However, when benchmarking this, using jQuery here it doesn't make any significative difference. Anyways, I find this pretty and more concise:

var elem = $('#container ul');
sidney
  • 2,704
  • 3
  • 28
  • 44