0

Hello actually how to make a 'Load more' button each 6 div with jquery ?

<div>1</div>

<div>2</div>

<div>3</div>

<div>4</div>

<div>5</div>

<div>6</div>

<a href="#" onclick="" > Load More .. </a>

and then show the rest div 7, 8, 9, etc

thanks

chridam
  • 100,957
  • 23
  • 236
  • 235
  • 1
    Are rest divs present in HTML or you wanna load them via ajax ? – Mohebifar Jun 17 '14 at 07:18
  • @user3747421 : Have a look at my answer. Hope it will help you to solve your problem. – SpiderCode Jun 17 '14 at 08:03
  • possible duplicate of [jQuery load first 3 elements, click "load more" to display next 5 elements](http://stackoverflow.com/questions/17736786/jquery-load-first-3-elements-click-load-more-to-display-next-5-elements) – mplungjan Jun 17 '14 at 08:17

2 Answers2

1

Use .append() in JQuery :

Define a function to be called on button click and in the function append divs like this :

function add(i) { $( "#container" ).append( "<div>"+i+"</div>" ); $( "#container" ).append( "<div>"+(i+1)+"</div>" ); $( "#container" ).append( "<div>"+(i+2)+"</div>" ); }

PG1
  • 1,220
  • 2
  • 12
  • 27
0

Approach 1: If you have fixed number of Divs in your HTML and initially show first 6 and then show rest of them by clicking on button.

<div id="container">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
    </div>
    <div id="containerHidden" style="display: none">
        <div>7</div>
        <div>8</div>
        <div>9</div>
    </div>
    <a href="#" id="loadMore">Load More .. </a>

    <script type="text/javascript" language="javascript">
        $('#loadMore').click(function () {
            $('#containerHidden').show();
        });
    </script>

Approach 2: If you want to append N number of divs dynamically every time by clicking on the button.

<div id="container">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
</div>

<a href="#" id="loadMore">Load More .. </a>

<script type="text/javascript" language="javascript">
    $('#loadMore').click(function () {
        for (var i = 0; i < 3; i++) {
            var counter = parseInt($('#container div').length);
            counter++;
            $('#container').append('<div>' + counter + '</div');
        }
    });
</script>
SpiderCode
  • 10,062
  • 2
  • 22
  • 42
  • The load more function will load each 6 div, not show or hide, when it 6 or not – user3747421 Jun 17 '14 at 08:10
  • Why the need for a hidden container? Just hide them all, show the first 6 on load and then show more each time – mplungjan Jun 17 '14 at 08:11
  • @user3747421 : what you exactly want ? What I understood is for example you have 10 divs. Initially you want to display only 6 and by clicking on load more button, rest should be displayed. Am I right or not ? – SpiderCode Jun 17 '14 at 08:14
  • Why even having a hidden container? Just append 6 more dynamically created containers so enable infinite creation of containers. – Magnus Jun 17 '14 at 08:14
  • @MagnusBurton : So you mean OP wants to add 6 more divs by clicking on 'Load More' button ? – SpiderCode Jun 17 '14 at 08:18
  • Yes. Like this: http://stackoverflow.com/questions/17736786/jquery-load-first-3-elements-click-load-more-to-display-next-5-elements – mplungjan Jun 17 '14 at 08:18
  • @SpiderCode That's correct. Parag's answer seems to do answer the question. – Magnus Jun 17 '14 at 08:36
  • @MagnusBurton : Have a look at my original answer before editing. :) – SpiderCode Jun 17 '14 at 08:38
  • @SpiderCode What your code does is to only show the next 3 rows. As I understand his question "every 6 rows" he wants a show more button to load another 6 rows.. Might be mistaken though – Magnus Jun 17 '14 at 08:41
  • @MagnusBurton : Right. I guess question seems to be confusing. Any ways thanks for your point :) – SpiderCode Jun 17 '14 at 08:45
  • @SpiderCode Yeah question is poorly asked. Sorry if I sounded a bit harsh above, not my intention. Have a great day! – Magnus Jun 17 '14 at 08:51