Search code examples
javascriptjqueryhtmldomjquery-traversing

How can I retrieve the li tag of an unordered list and put these tags into an array using JQuery?


I am pretty new in JQuery and I have the following problem.

Into a page I have the following unordered list:

<ul id="menu-language" class="nav">
    <li id="menu-item-422" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-422">
        <a href="http://localhost/onyx/prova/">prova cat</a>
    </li>

    <li id="menu-item-185" class="qtranxs-lang-menu qtranxs-lang-menu-it menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-185">
        ..............................
        SOMETHING
        ..............................
    </li>
</ul>

I need to create a JQuery script that retrieve all the li tag (and the related HTML content of these tags) and that put it into something like an array (so each position of this array have to contain a li tag and its HTML content).

What is the smarter way to do it using JQuery?

Tnx


Solution

  • $('#menu-language > li') will return an array of li nodes, you can get the content of each li element by iterating:

    $('#menu-language > li').each(function() {
    console.log($(this).html());
    });