Can you show me the way to open a single card?

Can someone assist me in making it so only one card opens when clicked, rather than all of them opening at once? Additionally, if there is already an open card and I click on another one, the currently open card should close automatically. If you have any examples of similar card designs with a "show more" feature, please share those as well. Would this type of display be suitable for reviews, or would it be better to use a carousel instead? Thank you.

$(document).ready(function() {
  
  var descMinHeight = 20;
  var desc = $('.desc');
  var descWrapper = $('.desc-wrapper');

  // show more button if desc too long
  if (desc.height() > descWrapper.height()) {
    $('.more-info').show();
  }
  
  // When clicking more/less button
  $('.more-info').click(function() {
    
    var fullHeight = $('.desc').height();

    if ($(this).hasClass('expand')) {
      // contract
      $('.desc-wrapper').animate({'height': descMinHeight}, 'slow');
    }
    else {
      // expand 
      $('.desc-wrapper').css({'height': descMinHeight, 'max-height': 'none'}).animate({'height': fullHeight}, 'slow');
    }

    $(this).toggleClass('expand');
    return false;
  });

});
.container {
  display:flex;
  flex-wrap:wrap;
}

.desc-wrapper {
  margin: 0 auto;
  margin-bottom: 0px;
  max-height: 50px;
  overflow: hidden;
}

.more-info {
  display: none;
}

.more-info .less,
.more-info.expand .more {
  display: none;
}
.more-info.expand .less {
  display: inline;
}

.more-info:focus {
  outline: none;
}
span.glyphicon {
  margin-left: 3px;
}


figure.snip1204 {
  position: relative;
  overflow: hidden;
  margin: 10px;
  min-width: 220px;
  max-width: 225px;
  width: 100%;
}
figure.snip1204 * {
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="col-xs-12 col-md-4">
<figure class="snip1204">
<blockquote><div class="reviews-body">
  <div class="desc-wrapper">
    <div class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore </div>
  </div>

<button class="more-info send-btn"><span class="more">More</span><span class="less">Hide</span></button>
</blockquote>
</figure>
</div>

    <div class="col-xs-12 col-md-4">
<figure class="snip1204">
<blockquote><div class="reviews-body">
  <div class="desc-wrapper">
    <div class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore </div>
  </div>

  <button class="more-info send-btn"><span class="more">More</span><span class="less">Hide</span></button>
</blockquote>
</figure>
</div>


      <div class="col-xs-12 col-md-4">
<figure class="snip1204">
<blockquote><div class="reviews-body">
  <div class="desc-wrapper">
    <div class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore </div>
  </div>

  <button class="more-info send-btn"><span class="more">More</span><span class="less">Hide</span></button>
</blockquote>
</figure>
</div>

</div>

Answer №1

To locate the parent and then identify the children, you can use the following selector:

$(this).parent().children('.desc-wrapper') // ...

$(document).ready(function() {
  
  var descMinHeight = 20;
  var desc = $('.desc');
  var descWrapper = $('.desc-wrapper');

  // Display more button if description is too long
  if (desc.height() > descWrapper.height()) {
    $('.more-info').show();
  }
  
  // Toggle between showing more or less text
  $('.more-info').click(function() {
    
    var fullHeight = $('.desc').height();

    if ($(this).hasClass('expand')) {
      // Contract
      $(this).parent().children('.desc-wrapper').animate({'height': descMinHeight}, 'slow');
    }
    else {
      // Expand 
      $(this).parent().children('.desc-wrapper').css({'height': descMinHeight, 'max-height': 'none'}).animate({'height': fullHeight}, 'slow');
    }

    $(this).toggleClass('expand');
    return false;
  });

});
.container {
  display:flex;
  flex-wrap:wrap;
}

.desc-wrapper {
  margin: 0 auto;
  margin-bottom: 0px;
  max-height: 50px;
  overflow: hidden;
}

.more-info {
  display: none;
}

.more-info .less,
.more-info.expand .more {
  display: none;
}
.more-info.expand .less {
  display: inline;
}

.more-info:focus {
  outline: none;
}
span.glyphicon {
  margin-left: 3px;
}


figure.snip1204 {
  position: relative;
  overflow: hidden;
  margin: 10px;
  min-width: 220px;
  max-width: 225px;
  width: 100%;
}
figure.snip1204 * {
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="col-xs-12 col-md-4">
<figure class="snip1204">
<blockquote><div class="reviews-body">
  <div class="desc-wrapper">
    <div class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore </div>
  </div>

<button class="more-info send-btn"><span class="more">More</span><span class="less">Hide</span></button>
</blockquote>
</figure>
</div>

    <div class="col-xs-12 col-md-4">
<figure class="snip1204">
<blockquote><div class="reviews-body">
  <div class="desc-wrapper">
    <div class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore </div>
  </div>

  <button class="more-info send-btn"><span class="more">More</span><span class="less">Hide</span></button>
</blockquote>
</figure>
</div>


      <div class="col-xs-12 col-md-4">
<figure class="snip1204">
<blockquote><div class="reviews-body">
  <div class="desc-wrapper">
    <div class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore </div>
  </div>

  <button class="more-info send-btn"><span class="more">More</span><span class="less">Hide</span></button>
</blockquote>
</figure>
</div>

</div>

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

javascript unusual comparison between strings

I am working on an ajax function that is responsible for sending emails and receiving responses from the server in a JSON format with type being either success or error. $("#submit_btn").click(function(event) { event.preventDefault(); var post_d ...

Achieving Efficiency with Handlebars: Streamlining Remote Template Organization and

I am looking for a way to better organize my HB template by splitting it into different HTML files. In this case, I have created a file called helpers.html. This file contains two script tags: <script id='alert' type='text/template>... ...

What is the process for transferring a JSON data object to the server with JavaScript XMLHttpRequest?

When I attempt to send an XMLHttpRequest to a PHP server, I am encountering difficulties in getting the $_POST or $_REQUEST object to be filled with the data I am sending using JavaScript: var r = new XMLHttpRequest; r.open("POST", "http://url.com", true ...

Error 400 encountered while attempting to post multiple data points to MVC Core using Ajax

I am trying to send form data to my MVC controller using AJAX with JQuery, but I keep receiving a 400 error. This is how I am attempting to post the data: var count = $('#MediaList').children().length; for (var i = 0; i < coun ...

a guide to structuring REST API requests with axios in Vue.js

In my Vue.js app, I am utilizing Axios to consume a REST API. Each time I make an API call, I create a new instance of Axios with the necessary authentication headers by calling axios.get in various places. // UserdataComponent.vue const anInstance = axio ...

Guide on transferring Javascript array to PHP script via AJAX?

I need to send a JavaScript array to a PHP file using an AJAX call. Here is the JavaScript array I am working with: var myArray = new Array("Saab","Volvo","BMW"); This JavaScript code will pass the array to the PHP file through an AJAX request and displ ...

The challenge of incorporating multiple stylesheets into an express/node/ejs application: encountering the error "Undefined style."

I am working on a project using express, node, and ejs and I want to be able to use different stylesheets for different views. In order to render a specific view with its corresponding style, I have included the following in my app.js file: app.get('/ ...

Omit specific module from a webpack 4 chunk

Is there a way to exclude a specific module from being included in a chunk using webpack 4? For example, let's say I do not want lodash to be included in the vendor file at all costs. How can this be achieved? Here is the current configuration: spli ...

Using Special Characters in React JS Applications

When handling CSV uploads with accented characters such as émily or ástha, I encountered the need to encode and pass them to the backend. Experimenting with different approaches, I tried adjusting the file type in FormData from 'text/plain' to ...

Exploring CSS Shapes: Unveiling the secrets behind the star. What's the mechanism at

https://i.stack.imgur.com/0BgQr.png Take a look at the code snippet below showcasing The Shapes of CSS. I'm interested in delving into the intricacies of these CSS properties. How exactly do shapes in CSS function? This includes pseudo CSS, borders, ...

Automatic Expansion Feature for HTML Tables

http://jsfiddle.net/bzL7p87k/ In my table, placeholders are filled with special words. However, what happens when I have more than 4 rows? For instance, if there are 21 placeholders for 21 rows? What I mean is this: I only have one row with a placeholder ...

Effortlessly glide to a specific element on an HTML page

I am working with XML data that contains a lot of Test information. I am using XSLT to display this data as HTML in a web browser. Each test includes a table with the test steps, and every time a test is executed, the table updates to show whether each s ...

Next.js lacks proper Tree Shaking implementation for MUI

After setting up a react app with next.js, I noticed that the bundle size for the client significantly increased when importing MUI components. Specifically, there was a large module called @mui/base added to the bundle, even though I am only using three M ...

Can you please adjust the image to the right side?

I'm having trouble aligning the image to the left in my code. It always seems to leave a margin on the sides that I can't figure out how to get rid of. https://i.sstatic.net/kKwII.jpg This is the snippet of my code: <section class="conta ...

the process of configuring date string for x-axis labels in chartjs

I recently created a time scale chart using chart.js. However, I now want to change the labels in my data array to be in the format of 01-02-2017, 02-06-2017 instead of "4 February 2017", "9 February 2017" and so on. Below is my code: var aR = null; va ...

Executing Statements in a Specific Order with Express and Sqlite3

I am having an issue creating a table and inserting an item into it using the node command. Despite my efforts to reorganize my script, the item is being inserted before the table is created. Interestingly, manually inputting the commands in sqlite3 works ...

Oops! Looks like there's an unexpected error with the module 'AppRoutingModule' that was declared in the 'AppModule'. Make sure to add a @Pipe/@Directive/@Component annotation

I am trying to create a ticket, but I encountered an error. I am currently stuck in this situation and receiving the following error message: Uncaught Error: Unexpected module 'AppRoutingModule' declared by the module 'AppModule'. Plea ...

Can jQuery be used to postpone the application of a CSS property?

Here is the code I am currently working with: <img class="animated fadeIn" src="https://example.com/img/image.png" style="background-image: url('.$this->url.'/thumb.php?src='.$row['background'].'&t=b&w=800&h ...

Is there a way to make res.render in node.js/express/mongo wait until the database search is finished before loading?

Having some difficulty with loading a table properly on my page because the information is not being passed to the ejs template before the page loads. I'm fairly new at this and could use some assistance! Just a heads up, owneditems consists of an ar ...

Trigger a specific directive instance through a function call when a key is pressed on the

Is it possible to trigger a specific directive instance's function when a global keyboard shortcut is pressed and it has the class "focused" based on ng-class? Below is some template code: home.html <body ng-controller="SampleController"> ...