javascript - If submenu li has more than 5 li's create a new ul and place remaining list items? -


i trying check if submenu has more 5 list items, , if grab remaining list item's , place them inside new ul outside of current parent ul using jquery. gets complicated because of structure of list.

here dom structure:

<ul id="nav" class="se test"> <li id="menu1" class="page-1307 parent-menu parent">     <div class="nav-inner">         <a class="menulink" id="menuitem1" onclick="return false" href="#">test<span class="toggle"></span></a>         <ul id="ie1" class="plain">             <li class="parent-menu parent"><a href="test">test<span class="toggle"></span></a>                 <div class="submenu-wrapper">                     <ul class="plain">                         <li><a href="test">test</a></li>                         <li><a href="test">test</a></li>                         <li><a href="test">test</a></li>                         <li><a href="test">test</a></li>                         <li><a href="test">test</a></li>                         <li><a href="test">test</a></li>                         <li><a href="test">test</a></li>                     </ul>                 </div>            </li>            <li class="parent-menu parent"><a href="test">test<span class="toggle"></span></a>                 <div class="submenu-wrapper">                     <ul class="plain">                         <li><a href="test">test</a></li>                         <li><a href="test">test</a></li>                         <li><a href="test">test</a></li>                         <li><a href="test">test</a></li>                     </ul>                </div>            </li>         </ul>     </div> </li> <li id="menu2" class="menulink page-7">     <div class="nav-inner">         <a href="test">test</a>     </div> </li> </ul> 

basically need grab remaining list items , place them in new li.parent-menu.parent includes children div.sub-menu-wrapper , ul.plain. actual remaining list items go inside ul.plain of new li.parent-menu. hope thi makes since. have been stuck on day or 2 , unble figure out. apprecitated, thank you.

this striving for, keep in mind dynamic.enter image description here

you can:

  1. loop ul in document
  2. foreach element count children
  3. if found li number under ul element > 5
  4. create new list html of required list

$(document).ready(function(){      $('.submenu-wrapper').each(function(){          var count_li=0;           var i=1;          $(this).children('ul').children('li').each(function(){              count_li++;              if(count_li>5 && i==1){                 $(document.body).append('<ul id="newlist"></ul>');                 $('#newlist').append($(this).nextuntil($(this).last()).andself());                 i++;              }           });                });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>  <ul id="nav" class="se test">  <li id="menu1" class="page-1307 parent-menu parent">      <div class="nav-inner">          <a class="menulink" id="menuitem1" onclick="return false" href="#">test<span class="toggle"></span></a>          <ul id="ie1" class="plain">              <li class="parent-menu parent"><a href="test">test<span class="toggle"></span></a>                  <div class="submenu-wrapper">                      <ul class="plain">                          <li><a href="test">test11</a></li>                          <li><a href="test">test12</a></li>                          <li><a href="test">test13</a></li>                          <li><a href="test">test14</a></li>                          <li><a href="test">test15</a></li>                          <li><a href="test">test16</a></li>                          <li><a href="test">test17</a></li>                      </ul>                  </div>             </li>             <li class="parent-menu parent"><a href="test">test<span class="toggle"></span></a>                  <div class="submenu-wrapper">                      <ul class="plain">                          <li><a href="test">test21</a></li>                          <li><a href="test">test22</a></li>                          <li><a href="test">test23</a></li>                          <li><a href="test">test24</a></li>                      </ul>                 </div>             </li>          </ul>      </div>  </li>  <li id="menu2" class="menulink page-7">      <div class="nav-inner">          <a href="test">test</a>      </div>  </li>  </ul>


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -