0

Trying to cobble together some code to create a "Load More" button for a JSON array i'm pulling down and dynamically populating the page with, using the jquery slice method. Still terrible at jquery would appreciate any assistance with this. Thanks so much.

Code:

function populateButtons(data) {
    var rightcontent = $(".rightcontent");
    var sub = document.createElement("a");
    var gh_buttons =  $(".rightcontent a")
    sub.innerHTML = data.title;
    sub.setAttribute("href", data.absolute_url);
    sub.setAttribute("target", "blank");
    sub.setAttribute("class", "hidden");
    $(rightcontent).append(sub);
    setTimeout(function() {
        $(gh_buttons).slice(0,10).removeClass("hidden");
    }, 100);
}
var populateButtons = gh_loadMore;
gh_loadMore = function() {
    var loadMore = function() {
        $(rightcontent).after("<button>Load More</button>");
    };
    $(loadMore).on('click', function (e) {
    e.preventDefault();
    $(gh_buttons).slice(10, 10).slideDown();
    $('html,body').animate({
        scrollTop: $(this).offset().top
    }, 1500);
    });
};
gh_loadMore();
5
  • so where are you stuck? Sorry but the question is very broad. It's not clear what you've tried to do already Commented Aug 15, 2017 at 21:08
  • So... what's currently wrong with the code? Commented Aug 15, 2017 at 21:08
  • why dont you give id to your button and select it with its id ? Commented Aug 15, 2017 at 21:09
  • Sorry, firstly, the console is saying that "gh_loadMore isn't defined" Commented Aug 15, 2017 at 21:09
  • @pjldesign you should define gh_loadMore before assign it to another variable Commented Aug 15, 2017 at 21:16

1 Answer 1

1

There is an order problem for your code, you can implement like following.

function populateButtons(data) {
    var sub = document.createElement("a");
    sub.innerHTML = data.title;
    sub.setAttribute("href", data.absolute_url);
    sub.setAttribute("target", "blank");
    sub.setAttribute("class", "hidden");
    $(".rightcontent").append(sub);

    var gh_buttons =  $(".rightcontent a");
    setTimeout(function() {
        $(gh_buttons).slice(0,10).removeClass("hidden");
    }, 100);
}

var gh_loadMore = function() {        
    $(".rightcontent").after("<button id='btn'>Load More</button>");
};

populateButtons(yourdata);  // call it with yourdata variable  
gh_loadMore(); 

$("#btn").on('click', function (e) {
     e.preventDefault();
     $(".rightcontent a").slice(10, 20).removeClass("hidden");
     $(".rightcontent a").slice(10, 20).slideDown();
     $('html,body').animate({
         scrollTop: $(this).offset().top
     }, 1500);
});
Sign up to request clarification or add additional context in comments.

7 Comments

getting "gh_buttons" not defined... assumed I was picking up the variable from the previous function...
thanks. i think i may be taking the wrong path with this -- all the JSON objects returned have been replaced with "Load More" buttons... :\
@pjldesign i have changed last updated code, there were many misused code line
thanks. happily the button is being painted and sliding the page but unfortunately not applying the new slice method...
@pjldesign You mean slice(10,10) part? did you check the console, is there any error
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.