0

I have a list of elements where i want to show max 5 elements and add show more button if total elements is more than 5. Show/Hide part is done but I am stuck to customize this list using jquery.

For Example here is a list of brands which having total 13 items.

<ul class="search-filter" id="attributeLevel1Facet">
   <li>brand1</li>
   <li>brand2</li>
   <li>brand3</li>
   <li>brand4</li>
   <li>brand5</li>
   <li>brand6</li>
   <li>brand7</li>
   <li>brand8</li>
   <li>brand9</li>
   <li>brand10</li>
   <li>brand11</li>
   <li>brand12</li>
   <li>brand13</li>
</ul>

I want to make this list like this using jquery only if total item is more than 5

<ul class="search-filter" id="attributeLevel1Facet">
   <li>brand1</li>
   <li>brand2</li>
   <li>brand3</li>
   <li>brand4</li>
   <li>brand5</li>
   <li class="search-opt hide">
      <ul class="search-opt">
       <li>brand6</li>
       <li>brand7</li>
       <li>brand8</li>
       <li>brand9</li>
       <li>brand10</li>
       <li>brand11</li>
       <li>brand12</li>
       <li>brand13</li>
      </ul>
    </li>         
</ul>
<c:if test="${fn:length(***) gt 5}">
    <a data-role="more-options" class="more-option" title="more-option">More Options</a>
</c:if>

I want to change the first list to second list using jquery if items > 5. If $("#attributeLevel1Facet > li").length > 5 then it should wrap it with another <ul><li> element and add more-option button (second list above).

Please help me.

Free-Minded
  • 5,322
  • 6
  • 50
  • 93
  • possible duplicate of [jQuery wrap code after x number of elements](http://stackoverflow.com/questions/2485479/jquery-wrap-code-after-x-number-of-elements) – valar morghulis Apr 08 '15 at 06:02

4 Answers4

1

You can check the length of the li elements and then create a new ul like

var $lis = $('#attributeLevel1Facet li');
if ($lis.length > 5) {
    var $ul = $('<ul class="search-opt"/>').append($lis.slice(5));
    $ul.wrap('<li class="search-opt hide" />').parent().appendTo('#attributeLevel1Facet')
}

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

You can use something like this,

$("#attributeLevel1Facet > li").filter(function() {
    return $(this).index() > 4;
}).wrapAll("<li class='search-opt hide'><ul class='search-opt'></ul></li>");

Fiddle

In the above code, filter will return the li elements whose index is greater than 4. Then you can wrap them with any elements.

Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53
0

Check this -> jQuery load first 3 elements, click "load more" to display next 5 elements

From the answer i think you can get what you want

Community
  • 1
  • 1
Robin
  • 854
  • 8
  • 20
0

You can try something like this

if($('li').length > 5){
    var element='<li class="search-opt hide">'+
      '<ul class="search-opt">';
    $('li:nth-child(5)').after(element)
    
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<ul class="search-filter" id="attributeLevel1Facet">
   <li>brand1</li>
   <li>brand2</li>
   <li>brand3</li>
   <li>brand4</li>
   <li>brand5</li>
   <li>brand6</li>
   <li>brand7</li>
   <li>brand8</li>
   <li>brand9</li>
   <li>brand10</li>
   <li>brand11</li>
   <li>brand12</li>
   <li>brand13</li>
</ul>
Akshay
  • 14,138
  • 5
  • 46
  • 70