kevas
kevas

Reputation: 563

Select elements from first level

I have this ul li list:

<ul>
  <li><div>Text</div><a href="#">A</a></li>
  <li><div>Text</div><a href="#">B</a></li>
  <li><div>Text</div><a href="#">C</a></li>
  <li>
    <ul>
      <li><div>Text</div><a href="#">A1</a></li>
      <li><div>Text</div><a href="#">B1</a></li>
      <li><div>Text</div><a href="#">C1</a></li>
      <li><div>Text</div><a href="#">D1</a></li>
    </ul>
  </li>
  <li><div>Text</div><a href="#">E</a></li>
</ul>

I need to choose all "li" element from the first level.

In this example it will be:

  <li><div>Text</div><a href="#">A</a></li>
  <li><div>Text</div><a href="#">B</a></li>
  <li><div>Text</div><a href="#">C</a></li>
  <li><div>Text</div><a href="#">E</a></li>

Trying to use:

  $('ul').children()

I get all "li" elements from the first level and second level.

Thank you very much.

Upvotes: 0

Views: 1350

Answers (6)

Research
Research

Reputation: 11

You can use following code which will search for 1st ul and then all li's: $('ul:first>li')

Upvotes: 1

Michael
Michael

Reputation: 496

You could change your selector to

$("'whatever element is above first level ul element' > ul > li")

Upvotes: -1

Okky
Okky

Reputation: 10466

Select li of ul:first which doesnot has a ul element as child.

$('ul:first').children('li:not(:has(ul))');

Fiddle

Upvotes: 4

Rahul Munjal
Rahul Munjal

Reputation: 2703

try

$('ul>li').css(//your doing//);

This will select the direct childs of the all ul tags in your code.

If you want to acess specific ul, try givig it a class or id.

Upvotes: 0

Ali insan Soyaslan
Ali insan Soyaslan

Reputation: 846

Give the external ul a class:

<ul class="my_ul">
....
</ul>

and in jquery you an get them like:

  $('.my_ul').children()

EDIT: my first code was getting al children.

Upvotes: -1

Jo&#227;o Neto
Jo&#227;o Neto

Reputation: 1

You can add a class to the first ul and then call .children()

Upvotes: 0

Related Questions