What is the best way to display two cards while concealing a third one using a "show more"

Is there a way to hide certain cards by default and only show them when the user clicks on a "View more" button? I have multiple cards, but I want to hide the last one initially and reveal it when the button is clicked.

I would really appreciate any assistance with this. Thank you!

Best regards, Bill

$(".card-border").on("click", function() {
  // Toggle the div background color
  $(this).toggleClass("card-bg");
  // Find the button
  var btn = $(this).find(".btn");
  // Toggle classes for ONE button
  btn.toggleClass('btn-add-item btn-rmv-item');
  // Depending on a button's class, change its text
  (btn.hasClass("btn-rmv-item")) ? btn.text("Remove"): btn.text("Add this extra");
});
.card-border {
  border: 1px solid #c7c7c7;
  border-radius: .25rem;
  padding: 15px 18px 10px 18px;
  margin-bottom: 30px;
  cursor: pointer;
}

.card-bg {
  background-color: rgba(89, 211, 137, .08);
  border: 1px solid #59d389;
}

.upsell-pricing {
  float: right;
  font-size: 18px;
  font-weight: 600;
}

.upsell-text {
  font-size: 15px;
  margin-top: 10px;
  color: #333333;
}

div.ho-border:hover {
  border: 1px solid #59d389;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Fuel Replacement -->
<div class="card-border ho-border">
  <h4 class="float-left">Fuel replacement</h4>
  <div class="upsell-pricing">£49/trip</div>
  <div class="clearfix"></div>
  <div class="upsell-text">Save time and return the vehicle at any fuel level. The price include upto a full tank of petrol/gas.</div>
  <div class="mt-3 float-right">
    <button type="button" class="btn btn-add-item">Add this extra</button>
  </div>
  <div class="clearfix"></div>
</div>
<!-- Portable WiFi Hotspot -->
<div class="card-border ho-border">
  <h4 class="float-left">Portable WiFi hotspot</h4>
  <div class="upsell-pricing">£10/day</div>
  <div class="clearfix"></div>
  <div class="upsell-text">Get the luxury of portable WiFi hotspot on the go. The price include unlimited data usage for the day.</div>
  <div class="mt-3 float-right">
    <button type="button" class="btn btn-add-item">Add this extra</button>
  </div>
  <div class="clearfix"></div>
</div>
<!-- Post-trip Cleaning -->
<div class="card-border ho-border">
  <h4 class="float-left">Post-trip cleaning</h4>
  <div class="upsell-pricing">£30/trip</div>
  <div class="clearfix"></div>
  <div class="upsell-text">Return the vehicle without bothering about post-trip cleaning. This extra does not cover damages to the vehicle seat, stains, spills, and smoke burns.</div>
  <div class="mt-3 float-right">
    <button type="button" class="btn btn-add-item">Add this extra</button>
  </div>
  <div class="clearfix"></div>
</div>

Answer №1

If you want to simplify it, you can use the following code snippet:

$(document).ready(function() {
  var visibleElements = $(".card-border:visible");
  var hiddenElements = $(".card-border:not(:visible)");

  if (hiddenElements.length > 0) {
    $('.card-border:last').after('<button class="showMore">Show more</button>')
  }

  $(document).on("click", ".showMore", function() {
    hiddenElements.first().show();
    hiddenElements = $(".card-border:not(:visible)");
    if (hiddenElements.length == 0) {
      $(".showMore").hide();
    }
  });
});

In the CSS, you can include the following styles:

.card-border:not(:nth-child(-n+3)) { // This will show the first 2 elements
  display: none;
}

This setup allows you to automatically display a 'showMore' button if there are any hidden '.card-border' elements.

Answer №2

Add the class 'hideclass' to all the cards you wish to conceal. I hope this information is helpful. Thank you.

$('.hideclass').hide() //To hide additional content

$(".card-border").on("click", function() {
  // Toggle the div background color
  $(this).toggleClass("card-bg");
  // Find the button
  var btn = $(this).find(".btn");
  // Toggle classes for ONE button
  btn.toggleClass('btn-add-item btn-rmv-item');
  // Depending on a button's class, change its text
  (btn.hasClass("btn-rmv-item")) ? btn.text("Remove"): btn.text("Add this extra");
});

$(".show").on("click", function() {
 
  $(this).hide()
  $('.hideclass').show()
 

});
.card-border {
  border: 1px solid #c7c7c7;
  border-radius: .25rem;
  padding: 15px 18px 10px 18px;
  margin-bottom: 30px;
  cursor: pointer;
}

.card-bg {
  background-color: rgba(89, 211, 137, .08);
  border: 1px solid #59d389;
}

.upsell-pricing {
  float: right;
  font-size: 18px;
  font-weight: 600;
}

.upsell-text {
  font-size: 15px;
  margin-top: 10px;
  color: #333333;
}

div.ho-border:hover {
  border: 1px solid #59d389;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Fuel Replacement -->
<div class="card-border ho-border">
  <h4 class="float-left">Fuel replacement</h4>
  <div class="upsell-pricing">£49/trip</div>
  <div class="clearfix"></div>
  <div class="upsell-text">Save time and return the vehicle at any fuel level. The price includes up to a full tank of petrol/gas.</div>
  <div class="mt-3 float-right">
    <button type="button" class="btn btn-add-item">Add this extra</button>
  </div>
  <div class="clearfix"></div>
</div>
<!-- Portable WiFi Hotspot -->
<div class="card-border ho-border">
  <h4 class="float-left">Portable WiFi hotspot</h4>
  <div class="upsell-pricing">£10/day</div>
  <div class="clearfix"></div>
  <div class="upsell-text">Get the luxury of a portable WiFi hotspot on the go. The price includes unlimited data usage for the day.</div>
  <div class="mt-3 float-right">
    <button type="button" class="btn btn-add-item">Add this extra</button>
  </div>
  <div class="clearfix"></div>
</div>
<!-- Post-trip Cleaning -->
<div class="card-border ho-border hideclass">
  <h4 class="float-left">Post-trip cleaning</h4>
  <div class="upsell-pricing">£30/trip</div>
  <div class="clearfix"></div>
  <div class="upsell-text">Return the vehicle without worrying about post-trip cleaning. This extra does not cover damages to the vehicle seat, stains, spills, and smoke burns.</div>
  <div class="mt-3 float-right">
    <button type="button" class="btn btn-add-item">Add this extra</button>
  </div>
  <div class="clearfix"></div>
</div>


<div class="card-border ho-border hideclass">
  <h4 class="float-left">Post-trip test</h4>
  <div class="upsell-pricing">£30/trip</div>
  <div class="clearfix"></div>
  <div class="upsell-text">Return the vehicle without worrying about post-trip cleaning. This extra does not cover damages to the vehicle seat, stains, spills, and smoke burns.</div>
  <div class="mt-3 float-right">
    <button type="button" class="btn btn-add-item">Add this extra</button>
  </div>
  <div class="clearfix"></div>
</div>
<button class="show">Show 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

Adding rows to a Datatable using an object array in rows.add()

Attempting to refresh my current Datatable by fetching new data using an Ajax function. Despite trying various solutions from other sources, the table does not update as expected. The function being used is shown below: $.ajax({ url: "<?php echo s ...

The timestamp will display a different date and time on the local system if it is generated using Go on AWS

My angular application is connected to a REST API built with golang. I have implemented a todo list feature where users can create todos for weekly or monthly tasks. When creating a todo, I use JavaScript to generate the first timestamp and submit it to th ...

What is the best way to combine a hyperlink with a string in an Angular form?

Currently I am in the process of learning angular and experimenting with creating a list of websites that can be clicked on, similar to what you would find in a bookmark app. This is based on a todo example. https://github.com/LightYear9/ToDoList In orde ...

Setting up an Angular CLI project

In our Angular 2 application, we utilize native CSS components that contain some JavaScript files. These components have wrappers to convert them into Angular 2 components. Now, I am looking to create a new Angular 4 project using Angular CLI and incorpora ...

jsonAn error occurred while attempting to access the Spotify API, which resulted

Currently, I am working on acquiring an access Token through the Client Credentials Flow in the Spotify API. Below is the code snippet that I have been using: let oAuthOptions = { url: 'https://accounts.spotify.com/api/token', method: ' ...

Issue with Material-UI Dialog Reusable Component: No error messages in console and app remains stable

Here is a component I've created for reusability: import { Dialog, DialogContent, DialogContentText, DialogTitle, Divider, Button, DialogActions, } from "@mui/material"; const Modal = ({ title, subtitle, children, isOpen, hand ...

Enhance the dropdown menu by incorporating a caret icon

Click here for the image, I am trying to incorporate the Bootstrap caret class into my select dropdown menu. .select_menu{ font-size: 12px; outline: none; border: thin #ddd solid; -webkit-appearance: none; -moz-appearance: none; appearance: ...

Troubleshooting issues with jQuery background-position animation and CSS sprites functionality

Trying to add animation to my navigation links background using a Css sprite. Check out the picture here: $(document).ready(function(){ $('nav a') // Move the background on hover .mouseover(function(){ $(this).stop().animate({ba ...

performing a query on two tables with sequelize

There must be a more efficient way to accomplish this task with fewer lines of code. I am not very experienced with databases and I am new to sequelize and node. The user id is passed as a parameter and I need to check if there is a corresponding user in ...

Looking for a jQuery fix: How can I deactivate specific items once an item is selected?

I'm currently working on a form where I need certain options to be disabled when a specific option is selected. I'm new to jQuery and could use some assistance, thank you. Check out the link here: My goal is to disable some options in the secon ...

Expand the accordion to reveal all the content

I'm facing an issue with my accordion where a scrollbar appears in every content section. To fix this, I tried setting overflow: hidden in the CSS: .ui-accordion .ui-accordion-content { padding: 1em 2.2em; border-top: 0; overflow: hidd ...

Maintain the webpage content even after submitting a form with a file attachment on the same page

I am in the process of creating a secure webpage exclusively for our members. This page will allow members to access their personal data stored in the database and upload files as needed. One concern I have is how to return to the member page with all the ...

Here is a unique rewrite:"Adjusting the prop of a Material UI Button component depending on screen size breakpoints can be achieved by utilizing

While using the Material UI Button component, I encountered an issue with conditionally determining the variant of the button based on screen size. Specifically, I want the variant to be 'outlined' on medium and larger screens, and no variant at ...

Enhance your WordPress site by implementing infinite scroll with AJAX to seamlessly load more posts

Currently, I have implemented a feature that loads more posts through AJAX when the user clicks on a 'load more' button. The code I utilized is based on a tutorial found at this link. My goal now is to enhance this functionality so that instead ...

The Adonis 5 build failed to transfer the environment file to the designated build directory

During my experience with Adonis 5 in production, I consistently encountered an issue where the .env file failed to copy into the build folder when I attempted to run `npm run build`. Is this still considered a bug in the system? ...

Retrieve the ID of the image element using Jquery from a collection of images within a div container

I'm encountering a simple issue that I can't seem to solve. I am working on a basic slider/gallery with the following functionalities: 1) "If button 1 is clicked, image one will appear." 2) "Clicking on button 2 will make IMAGE 1 slide left and I ...

Even after being removed, the input field in Firefox stubbornly maintains a red border

I have a project in progress that requires users to input data on a modal view and save it. The validation process highlights any errors with the following CSS snippet: .erroreEvidenziato { border: 1px solid red; } Here is the HTML code for the moda ...

Issues with footers in IE 10/11 and Edge when using Grid layout

In the latest versions of Chrome, Firefox, Opera, IE 10/11 and Edge, the Grid layout below works perfectly fine. The only issue is with the Microsoft browsers where the footer fails to start scrolling when the content exceeds the screen size, leaving it fi ...

Is it possible to navigate to the Next page using a different button instead of the pagination controls

Is it possible to navigate to the next page upon clicking a button labeled "Press me"? Here is the code snippet: <!doctype html> <html ng-app="plunker"> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/an ...

Avoiding memory leaks in Reactjs when canceling a subscription in an async promise

My React component features an async request that dispatches an action to the Redux store from within the useEffect hook: const fetchData = async () => { setIsLoading(true); try { await dispatch(dataActions.fetchData(use ...