To view the following set of three images, simply click on the "load more" button

I'm looking to add a load more button to reveal additional images. Currently, the page loads with 3 images visible, and upon clicking the load more button, the next set of 3 images should be displayed on the screen.

Unfortunately, the code I've attempted below isn't functioning as expected. Can someone assist me in resolving this issue?

$(function() {
  $(".item").slice(0, 2).show(); // display the initial three
  $("#load").click(function(e) { // click event for loading more
    e.preventDefault();
    $(".item:hidden").slice(0, 2).show(); // show the next hidden divs
    if ($(".item:hidden").length == 0) { // check for any remaining hidden divs
      alert("No more divs"); // display an alert if there are none left
    }
  });
});
.lightgallery1 a {
  width: 30.33%;
  margin: auto;
  display: inline-block;
  padding: 0px 20px 20px 0;
}

.lightgallery1 a img {
  width: 100%;
}

a {
  display: none;
}
<div class="lightgallery1">
  <a href="https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg"><img src="https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg"></a>

  <!-- Additional image links go here -->

</div>

<a href="#" id="load">Load More</a>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Answer №1

Your css needed adjustment. Try this:

.lightgallery1 a {
  display: none;
}

Your current code displays 2 images. If you want to display 3, change .slice(0, 2) to .slice(0, 3)

$(function() {
  $(".item").slice(0, 2).show(); // choose the first two
  $("#load").click(function(e) { // click event for loading more
    e.preventDefault();
    $(".item:hidden").slice(0, 2).show(); // select the next 10 hidden divs and show them
    if ($(".item:hidden").length == 0) { // check if there are any hidden divs left
      alert("No more divs"); // alert if there are none left
    }
  });
});
.lightgallery1 a {
  padding: 0px 20px 20px 0;
  display: none;
}

.lightgallery1 a img {
  width: 33%;
}
<div class="lightgallery1">
  <a href="https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg"><img src="https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg"></a>

  <a href="https://images.freeimages.com/images/large-previews/851/poppies-1369329.jpg"><img src="https://images.freeimages.com/images/large-previews/851/poppies-1369329.jpg"></a>

  <a href="https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg" class="item"><img src="https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg"></a>

  <a href="https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg" class="item"><img src="https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg"></a>

  <a href="https://images.freeimages.com/images/large-previews/fe6/midsummer-fields-1-1394719.jpg" class="item"><img src="https://images.freeimages.com/images/large-previews/fe6/midsummer-fields-1-1394719.jpg"></a>

  <a href="https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg" class="item"><img src="https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg"></a>

  <a href="https://images.freeimages.com/images/large-previews/fe6/midsummer-fields-1-1394719.jpg" class="item"><img src="https://images.freeimages.com/images/large-previews/fe6/midsummer-fields-1-1394719.jpg"></a>

  <a href="https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg" class="item"><img src="https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg"></a>

  <a href="https://images.freeimages.com/images/large-previews/851/poppies-1369329.jpg" class="item"><img src="https://images.freeimages.com/images/large-previews/851/poppies-1369329.jpg"></a>

  <a href="https://images.freeimages.com/images/large-previews/851/poppies-1369329.jpg" class="item"><img src="https://images.freeimages.com/images/large-previews/851/poppies-1369329.jpg"></a>

  <a href="https://images.freeimages.com/images/large-previews/851/poppies-1369329.jpg" class="item"><img src="https://images.freeimages.com/images/large-previews/851/poppies-1369329.jpg"></a>

</div>
<div>
<a href="#" id="load">Load More</a>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Answer №2

Give this method a try:

var links = [
    "https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg",
  "https://images.freeimages.com/images/large-previews/851/poppies-1369329.jpg",
  "https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg",
  "https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg",
  "https://images.freeimages.com/images/large-previews/fe6/midsummer-fields-1-1394719.jpg",
  "https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg",
  "https://images.freeimages.com/images/large-previews/fe6/midsummer-fields-1-1394719.jpg",
  "https://galleria.io/wp-content/themes/galleria/dist/images/demo/4m.jpg",
  "https://images.freeimages.com/images/large-previews/851/poppies-1369329.jpg",
  "https://images.freeimages.com/images/large-previews/851/poppies-1369329.jpg",
  "https://images.freeimages.com/images/large-previews/851/poppies-1369329.jpg",
];

var $gallery  = $(".lightgallery1").first();
var $loadMore = $("#load");

function loadAdditionalImages(e) {
  
  for(var i = 0; i < 3; i++) {
     
    if(links.length) {

      var src   = links.pop();
      var $link = $("<a>");
      var $img  = $("<img>");

      $img.attr("src", src);
      $link.attr("href", src).addClass("item");
      
      $link.append($img);
      $gallery.append($link);

    } else {
      $loadMore.hide();
    }
    
  }
  
}

$loadMore.on("click", loadAdditionalImages);
loadAdditionalImages();
.lightgallery1 a {
  width: 30.33%;
  margin: auto;
  display: inline-block;
  padding: 0px 20px 20px 0;
}

.lightgallery1 a img {
  width: 100%;
}

a {
 /* display: none; */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="lightgallery1"></div>

<button id="load">Load More</button>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

To ensure proper formatting, I must include a comma operator for numbers while typing and limit the decimal places to two

Could someone assist me in formatting a number to include commas and restrict the decimal places to two using regex? I have attempted the following, but need help making it work properly. Currently, when I enter a number it shows up as 1231244, however I ...

Running a synchronous jQuery ajax request from a form submit in CodeIgniter is not possible

My javascript function is being triggered twice, once when a user enters text in a field using jQuery.change and the second time when the form is submitted. The function should make an ajax request and then either halt or proceed with submitting the form b ...

Can someone provide guidance on utilizing the index correctly within this v-for to prevent any potential errors?

I am encountering an issue with using index in a v-for loop to implement a function that deletes items from an array. The linter is flagging "index is defined but never used" as an error. I am following the instructions provided in a tutorial, but I am un ...

Having trouble with the JOSN.parse function not functioning properly

Hello there, I'm currently attempting to extract data from a simple JSON string but encountering an error. The object I am trying to retrieve looks like this: { "heading" : "The movies", "box5" : "Click on icon to add text.", "box1" : "At the movies, ...

Personalized Django Sign-in

I am having difficulty accessing the password that needs to be transferred from my form. I need help figuring out where I might be making a mistake. I am looking to create a custom login page because I want to change how usernames are input. For now, I hav ...

I am having trouble with my :hover selector being interrupted by text, what steps should I take to resolve this

My issue revolves around an image with a hover effect that changes the box shadow color. However, there is text on the image that also changes when clicked, creating a problem where the area occupied by the text prevents the box shadow from displaying prop ...

Navigating through events within React

I'm working on creating a login page, where upon clicking the submit button it should navigate to the Dashboard page. However, I seem to be encountering an issue with my code. After entering login details and clicking on the login button, the details ...

Hovering triggers the appearance of Particle JS

I am attempting to add particle js as the background for my website. I have tried implementing this code snippet: https://codepen.io/nikspatel/pen/aJGqpv My CSS includes: position: fixed; z-index: -10; in order to keep the particles fixed on the screen e ...

Backbone and Laravel - Choose a squad and automatically create users for the selected team

I've recently started exploring backbone.js and have gone through Jeffery Way's tutorial on using Laravel and Backbone. As of now, I have a list of teams being displayed along with their ids fetched from the database. I have also set up an event ...

Refreshing the access token in Linkedin's oauth does not result in extending the expiration time

Currently, I am implementing a strategy to renew Linkedin OAuth2 access tokens. When starting the OAuth process in the browser, the dialogue is skipped and a new code is generated. This code is then used to acquire a fresh access_token that differs from t ...

Is it possible to automatically collapse the Bootstrap navbar when the viewport is not wide enough?

Currently, my navbar is styled with a "shrinkwrap" background to match the width of the content: <nav class="navbar fw-bold h5 rounded-5 navbar-expand d-inline-flex"> <div class="container-fluid"> ...

Managing Pop-Ups when browsing on Internet Explorer

I've been working on an Excel VBA macro that automates several tasks on the Medicare website. The macro opens IE, logs in, compares claims, and alerts me to any differences found. However, I've encountered a problem during the log-in step, specif ...

Gradually vanishing words while they glide across the screen

I want to achieve a similar effect seen in the game A Dark Room. The text in the game serves two purposes which I am trying to replicate: The new text is added above the old text instead of below it, pushing the older text down the page as the game progr ...

invoke a pop-up window from a separate document

How can I make a modal window open from another file? I can't seem to figure out why it's not working. file 1: <template> <section class="header"> <div class="header-container"> <div cl ...

The code snippet `document.getElementById("<%= errorIcon.ClientID %>");` is returning a null value

I need to set up validation for both the server and client sides on my registration page. I want a JavaScript function to be called when my TextBox control loses focus (onBlur). Code in the aspx page <div id="nameDiv"> <asp:Upd ...

Is there a definitive way to distinguish between scrollTop and scrollHeight in web development?

For instance, function checkingScroll(){ var newHeight = element.scrollHeight; var scrollTopValue = element.scrollTop; alert("The scrollHeight property is: " + newHeight + "px"); alert("The scrollTop property is: " + scrollTopValue ...

Prevent the float:left property from causing my div to overlap with another div

Is there a way to style "a" so that it can take up space and float to the left simultaneously? <div style="background:#111"> <div id="a" style="float:left; height:30px"></div> </div> ...

"By selecting the image, you can initiate the submission of the form

I am trying to figure out why clicking on the image in my form is triggering the form submission by default. Can someone please provide guidance on this issue? <form name="test1" action="er" method="post" onsubmit="return validateForm()" <input ty ...

Utilize dynamic global variables in React that are provided during runtime, making them unpredictable during the build process

I am currently developing an application for Overwolf, which you can find more information about at For this platform, applications are built using html/js and run in the Overwolf Browser. The browser provides access to the Overwolf API through a global v ...

Access the data attribute of a button element in AngularJS

Utilizing Angularjs for managing pagination loop button id=remove_imslide, I am attempting to retrieve the value of data-img_id from the button to display in an alert message using a Jquery function on button click, but I am encountering issues. This is h ...