0

This is example list :

<ul>
    <li>Item:
        <ol>
            <li>Point:
                <div>
                    <ul>
                        <li>elem1</li>
                    </ul>
                </div>
            </li>
        </ol>
    </li>
    <li>elem2</li>
</ul>
<ul>
    <li>Point:
        <div>
            <ul>
                <li>elem1</li>
            </ul>
        </div>
    </li>
</ul>
<ul>
    <li>simple list1</li>
</ul>

I have a code to find depth from unordered list :

var n = 0

$('ul').each(function(i){ 
    if (($(this).parents('ul').length + 1) > n) { n = $(this).parents('ul').length + 1; }
});

But what to do if more than one list and which are mixed list ?

EDIT : Here mixed list means ul list and ol list.

4
  • 4
    But what to do if more than one list and which are mixed list? What is mixed list? What is the expected output?
    – Tushar
    Nov 24, 2015 at 5:36
  • What is mixed list? Are you calling list inside a list a Mixed list ?
    – Mayank
    Nov 24, 2015 at 5:37
  • tell us what you considered as mixed list in the example list. Nov 24, 2015 at 6:16
  • 1
    @Tushar, By mixed I think he's mean to ol too. Mixed types of lists.
    – Mosh Feu
    Nov 24, 2015 at 6:18

1 Answer 1

1

The key is to add ol to the selector of the check.

var n = 0

$('ul, ol').each(function(i){ 
    if (($(this).parents('ul, ol').length + 1) > n) { n = $(this).parents('ul, ol').length + 1; }
});

$('#result').html(n);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
    <li>Item:
        <ol>
            <li>Point:
                <div>
                    <ul>
                        <li>
                          elem1
                          <ol>
                            <li>elem1_1</li>  
                          </ol>
                      </li>
                    </ul>
                </div>
            </li>
        </ol>
    </li>
    <li>elem2</li>
</ul>
<ul>
    <li>Point:
        <div>
            <ul>
                <li>elem1</li>
            </ul>
        </div>
    </li>
</ul>
<ul>
    <li>simple list1</li>
</ul>

<hr />

<div id="result"></div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.