Question

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?

Was it helpful?

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

OTHER TIPS

First you need to fix your HTML. You can't have ul elements inside other ul elements, they have to be inside the li elements:

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

To remove the lists and keep the links, move the links out of them and then remove the lists. For example:

$('ul>li>a').appendTo(document.body);
$('ul').remove();

Demo: http://jsfiddle.net/kRBRC/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top