Search code examples
jqueryhtml-listsdetach

Delete inner UL and Li elements with jQuery


I have structured UL/Li like this

<ul>
 <li><a>1.1</a></li>
 <li>.....</li>
 <li><a>1.N</a></li>
    <ul>
      <li><a>2.1</a></li>
         <ul>
           <li><a>3.1</a></li>
           .....
           <li><a>3.N</a></a></li>
         </ul>
    </ul> 
</ul>

I want to remove all UL and LI elements and keep all tags. My goal is get menu as multi row strings.

How I can do it with jQuery?


Solution

  • See if this helps you:

    Js:

    var tags = $("ul li").map( function(){
              return $(this).html();
            }).get();      // get all tags in an Array.  I suspect you meant Array by saying `multi row strings`
    $("#main ul").remove();
    
    $("#main").append(tags);
    

    Fot Html;

    <div id="main">
    <ul>
     <li><a>1.1</a></li>
     <li><a>1.N</a></li>
     <li><ul>
          <li><a>2.1</a></li>
           </li><ul>
               <li><a>3.1</a></li>
               <li><a>3.N</a></a></li>
             </ul>
           </li>
        </ul>
    </li>
    </ul>
    </div>
    

    Sample