Display the initial three image components on the HTML webpage, then simply click on the "load more" button to reveal the subsequent two elements

I've created a div with the id #myList, which contains 8 sub-divs each with an image.

My goal is to initially load the first 3 images and then have the ability to load more when clicking on load more.

I attempted to follow this jsfiddle example

Below is my code, similar to the example but not functioning for some unknown reason:

<div id="mylist" class="autoplay123company">

          <div class="col-lg-4 d-flex justify-content-center" style="" aria-hidden="true" tabindex="0">

            <img src=" http://realbusinessanalytics.com/wp-content/uploads/2018/07/gtbank__1.jpg" alt="" style="height:100px !important;" width="150" height="100">

          </div>
          <div class="col-lg-4 d-flex justify-content-center" style="" aria-hidden="true" tabindex="0">

            <img src=" http://realbusinessanalytics.com/wp-content/uploads/2018/07/partner-MTN.png" alt="" style="height:100px !important;" width="150" height="100">

          </div>
          <div class="col-lg-4 d-flex justify-content-center" style="" aria-hidden="true" tabindex="0">

            <img src=" http://realbusinessanalytics.com/wp-content/uploads/2018/07/Partner-airtel.png" alt="" style="height:100px !important;" width="150" height="100">

          </div>
         ...
        </div>
        <div id="loadMore">Load more</div>
        <div id="showLess">Show less</div>
#myList div.col-lg-4 {
  display: none;
}

#loadMore {
  color: green;
  cursor: pointer;
}

#loadMore:hover {
  color: black;
}

#showLess {
  color: red;
  cursor: pointer;
}

#showLess:hover {
  color: black;
}
...

I would appreciate any help in getting it to work. You can view my edited jsfiddle here

Answer №1

The reference in your JS and CSS is to #myList, but the actual ID of the div is mylist. Simply adjust the casing of the div ID and everything should function as intended.

Answer №2

Explaining a simple mistake involving capitalization in an ID #myList in HTML.

$(document).ready(function () {
    size_li = $("#myList div.col-lg-4").size();
    x=3;
    $('#myList div.col-lg-4:lt('+x+')').show();
    $('#loadMore').click(function () {
        x= (x+5 <= size_li) ? x+5 : size_li;
        $('#myList div.col-lg-4:lt('+x+')').show();
         $('#showLess').show();
        if(x == size_li){
            $('#loadMore').hide();
        }
    });
    $('#showLess').click(function () {
        x=(x-5<0) ? 3 : x-5;
        $('#myList div.col-lg-4').not(':lt('+x+')').hide();
        $('#loadMore').show();
         $('#showLess').show();
        if(x == 3){
            $('#showLess').hide();
        }
    });
});
#myList div.col-lg-4 {
  display: none;
}

#loadMore {
  color: green;
  cursor: pointer;
}

#loadMore:hover {
  color: black;
}

#showLess {
  color: red;
  cursor: pointer;
}

#showLess:hover {
  color: black;
}
        <div id="myList" class="autoplay123company">

          <div class="col-lg-4 d-flex justify-content-center" style="" aria-hidden="true" tabindex="2">

            <img src="http://realbusinessanalytics.com/wp-content/uploads/2018/07/gtbank__1.jpg" alt="" style="height:100px !important;" width="150" height="100">

          </div>
          <div class="col-lg-4 d-flex justify-content-center" style="" aria-hidden="true" tabindex="-2">

            <img src="http://realbusinessanalytics.com/wp-content/uploads/2018/07/partner-MTN.png" alt="" style="height:100px !important;" width="150" height="100">

          </div>
          <div class="col-lg-4 d-flex justify-content-center" style="" aria-hidden="true" tabindex="1">

            <img src="http://realbusinessanalytics.com/wp-content/uploads/2018/07/Partner-airtel.png" alt="" style="height:100px !important;" width="150" height="100">

          </div>
          <div class="col-lg-4 d-flex justify-content-center" style="" aria-hidden="true" tabindex="-1">

            <img src="http://realbusinessanalytics.com/wp-content/uploads/2018/06/page-1.png" alt="" style="height:100px !important;" width="150" height="100">

          </div>
          <div class="col-lg-4 d-flex justify-content-center" style="" aria-hidden="true" tabindex="3">

            <img src="http://realbusinessanalytics.com/wp-content/uploads/2018/07/Partner-citibank.png" alt="" style="height:100px !important;" width="150" height="100">

          </div>
          <div class="col-lg-4 d-flex justify-content-center" style="" aria-hidden="true" tabindex="-3">

            <img src="" alt="" style="height:100px !important;" width="150" height="100">

          </div>
          <div class="col-lg-4 d-flex justify-content-center" style="" aria-hidden="true" tabindex="-2">

            <img src="" alt="" style="height:100px !important;" width="150" height="100">

          </div>
          <div class="col-lg-4 d-flex justify-content-center" style="" aria-hidden="true" tabindex="-1">

            <img src="" alt="" style="height:100px !important;" width="150" height="100">

          </div>
        </div>
        <div id="loadMore">Load more</div>
        <div id="showLess">Show less</div>

Refer to the Working jsfiddle link. Credits to Macros

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

Tips for preventing repetition of code in multiple entry points in Rollup

My goal is to use rollup to process a group of input files and generate multiple output files in the dist directory that all have some common code shared between them. Below is my current rollup configuration: import path from 'path'; import pat ...

Can you explain the key distinction between the backtick (`) and the ampersand-hash-39

I am completely new to TypeScript, JavaScript, and Angular. As I follow some tutorials, I often encounter code snippets like the one below: class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { return `(${this.x}, ${th ...

Close session when browser/tab is exited

After extensive searching online, I have been unable to find a satisfactory solution for ending a session when a browser or tab is closed without requiring the user to log off. I have attempted numerous JavaScript codes that I came across, but none of the ...

What are the available choices for constructing HTML based on an ajax response?

Are there any alternatives or libraries available for constructing html from an ajax response? Currently, I am taking the json data received, creating the html as a string, and using a jQuery function to insert it into the DOM. However, I believe there mu ...

An Illustrative Example of Inheritance in Javascript?

I've been searching on various platforms for a straightforward example of inheritance without much luck. The examples I've come across are either too complex, obscure, or just not user-friendly. I have multiple service functions that all share so ...

I am hoping for JavaScript to perform three distinct actions depending on the names of the files that are currently open in Photoshop

Currently, I am in the process of developing a JavaScript script for Photoshop. The main objective of this script is to prompt the user to choose a folder containing multiple files. Subsequently, the script will open each file within the folder and perform ...

Selecting specific elements based on their position within a parent element using

I can't seem to figure out the strange behavior of the nth-child selector. After calling the function BackColor1(), my visual appears different than expected: Something seems off. However, when I call BackColor2(), everything looks normal again. Co ...

Mobile devices are unable to properly implement the Jquery show/hide feature

Currently, I am working on a small Russian project and facing some challenges with the mobile version of my website: php8098.github.io/breakfast. The issue lies within the first slider where the play button should trigger a modal window to open. I urgentl ...

In search of a mobile web UI framework solely built with CSS styling

Currently in search of a CSS framework that specializes in styling form elements and lists for optimal small screen use. The ideal framework must be CSS only, requiring minimal to no JavaScript. Something along the lines of Twitter Bootstrap would be per ...

Conceal dynamically generated divs based on their individual class identifiers

My task involves dynamically generating div elements with incremented class names. Each div contains a close button in the top right corner, and I need to hide a specific div when the close button is clicked. Example Code: var i=1; var newImageBoxdiv = $ ...

"Exploring the world of JavaScript through the lens of time

This summer, I have the opportunity to assist a friend with some JavaScript challenges on his website. The main issue seems to revolve around technical difficulties with online form submissions. Unfortunately, there are instances where we struggle to verif ...

How do you structure `en.js` or `ja.js` files for lazy loading in vue-i18n?

What is the correct format for lazy loading en.js or ja.js? The code below is not working: // en.js export default { title: 'Title', greeting: 'How are you' }; and import Vue from 'vue'; import Inven ...

Steps to generate an error in the 'response' function of a $httpProvider interceptor

I am currently working on creating a custom HTTP interceptor in Angular and I am looking to generate an error from the response of the $httpProvider interceptor. According to the provided documentation: response: Interceptors are triggered by the http re ...

Error: The function SomeFunction is not recognized as a valid function (Mongoose library)

Help needed! I'm encountering an error stating "TypeError: User.getUserByUsername is not a function at Strategy._verify (.../routes/users.js:65:10) var User = require('../models/user'); passport.use(new LocalStrategy( function(username, ...

Utilizing turbolinks enables the page to be reloaded upon form submission

Currently, I have implemented turbolinks in my Rails application. However, every time I submit a form, the page reloads and the form is submitted. Is there a way to prevent the page from reloading and also submit the form seamlessly? ...

What is the process for embedding HTML and CSS content into an Outlook email?

Has anyone experienced trouble adding an HTML and CSS web page to an Outlook email? I attempted to use the "Insert as text" option, but the CSS file is not loading correctly in Outlook. Any suggestions on how to fix this? The error message states that the ...

Leveraging a common element throughout various partial views

In my ASP.Net MVC 3 application, I have various controllers and actions that display pop-up dialog windows for user input. One important aspect of these dialogs is the faded mask that appears behind them. Each dialog window control resides in a separate P ...

What exactly does "nothing" mean in Node when using async await?

I have a method as shown below: private async sendToAll(clients) { for(const client of clients) { this.send(client, message); await true; // What should I put here to allow the rest of the application to continue executi ...

Adjust the color of a contenteditable div once the value matches

I currently have a table with some contenteditable divs: <div contenteditable="true" class="change"> This particular JavaScript code is responsible for changing the color of these divs based on their content when the page loads. However, I am now ...

Display loading spinners for multiple ajax requests

At the moment, I am displaying spinners using the ajax setup method call: (function() { $.ajaxSetup({ beforeSend: showLoader, complete: hideLoader, error: hideLoader }); })(); While this setup is functioning properly, the ...