Skip to content Skip to sidebar Skip to footer

Jquery Hide First 12 Elementes, Show Next 12 Elements

what i am trying to do is make the first 12 elements hidden and show the next 12 elements. //this is dynamic loaded content
).children('.front-pro:lt(12)').addClass('hidden'); $('.inner-content').children('.front-pro:gt(11)').removeClass('hidden'); $(".next").hide(); $(".prev").show(); } functionsearchPrev() { $('.inner-content').children('.front-pro:lt(12)').removeClass('hidden'); $('.inner-content').children('.front-pro:gt(11)').addClass('hidden'); $(".next").show(); $(".prev").hide(); }
.front-pro.hidden {
  display: none;
}
.prev {
  display: none;
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="inner-content"><divclass="front-pro">1</div><divclass="front-pro">2</div><divclass="front-pro">3</div><divclass="front-pro">4</div><divclass="front-pro">5</div><divclass="front-pro">6</div><divclass="front-pro">7</div><divclass="front-pro">8</div><divclass="front-pro">9</div><divclass="front-pro">10</div><divclass="front-pro">11</div><divclass="front-pro">12</div><divclass="front-pro hidden">13</div><divclass="front-pro hidden">14</div></div><divclass="next"onclick="searchNext();">next</div><divclass="prev"onclick="searchPrev();">prev</div>

I create a general solution after your comment with next and previous(I use step 3 for demo purposes but you can use what ever you want):

var pager = (function() {
  /*declare private variables*/var first = 0,
    last = 2,
    step = 3;

  functionsearchNext() {
    //next function//increasing first and last variables
    first += step;
    last += step;
    pagerHelper();
  }

  functionsearchPrev() {
    //previous function//decrease first and last variables
    first -= step;
    last -= step;
    pagerHelper();
  }

  functionpagerHelper() {
    //get all child elements with class front-pro //of element with class .inner-contentvar childElems = $(".inner-content .front-pro");
    //iterate through the elements
    childElems.each(function(i, el) {
      //show the elements that match the criteria removing css classif (i >= first && i <= last) {
        $(el).removeClass('hidden');
      } else {
        //hide rest
        $(el).addClass('hidden');
      }
    });
    nextPreviousToggle(childElems.length);
  }

  functionnextPreviousToggle(maxEle) {
    //here the code is to show/hide next/previous buttonsif (last >= maxEle) {
      $(".next").hide();
    } else {
      $(".next").show();
    }
    if (first === 0) {
      $(".prev").hide();
    } else {
      $(".prev").show();
    }
  }
  return {
    searchNext: searchNext,
    searchPrev: searchPrev
  }
})();
.front-pro.hidden {
  display: none;
}
.prev {
  display: none;
}
.prev:hover,
.next:hover {
  text-decoration: underline;
  cursor: pointer;
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="inner-content"><divclass="front-pro">1</div><divclass="front-pro">2</div><divclass="front-pro">3</div><divclass="front-pro hidden">4</div><divclass="front-pro hidden">5</div><divclass="front-pro hidden">6</div><divclass="front-pro hidden">7</div><divclass="front-pro hidden">8</div><divclass="front-pro hidden">9</div><divclass="front-pro hidden">10</div><divclass="front-pro hidden">11</div><divclass="front-pro hidden">12</div><divclass="front-pro hidden">13</div><divclass="front-pro hidden">14</div></div><spanclass="next"onclick="pager.searchNext();">next</span><spanclass="prev"onclick="pager.searchPrev();">prev</span>

References

:gt()

:lt()

Solution 2:

You use the following code to handle any number of divs,

var x = $(".inner-content div").hide();

$("div:contains(next)").click(function() {
  var cnt = $(this).data("cnt") || 0;
  if((cnt * 12) > x.length){ cnt = 0; }
  x.hide().filter(":eq("+ (cnt * 12)  + "), :lt(" + ((cnt * 12) + 12) + "):gt(" + (cnt * 12) + ")").show();
  $(this).data("cnt", ++cnt);
});

DEMO

Solution 3:

Try this instead

$('.inner-content').children().each(function (i, x) {
    if (i < 12) // Hide First 12 i.e 0-11
        $(x).addClass('hidden');
    elseif (i < 24) // Show Next 12 i.e 12-23
        $(x).removeClass('hidden');
})

Also make sure you have css rule defined as

.hidden {
    display: none;
}

Post a Comment for "Jquery Hide First 12 Elementes, Show Next 12 Elements"