Display or conceal content depending on the height of a div element

I need help with the following code. I want to hide the second paragraph by default and show it only when the blue circle is clicked and the div is expanded.

Here is the code:

What this code does is calculate the inner height of a specific element and store it in a data property. It then hides the second paragraph inside that element. When the downarrow is clicked, it animates the height of the element to either expand or collapse it. Additionally, it changes the icon inside the downarrow based on whether the element is expanded or collapsed. There seems to be an issue where the second paragraph should display when the element's height is greater than 95 pixels, but the current implementation is not working as expected.

Answer №1

I have made some modifications to your code to hide the 2nd paragraph (message) by default and reveal it with a slide effect upon clicking the blue circle.

Some of the styling on the paragraph seemed unnecessary, so I removed it. The parent div will automatically expand to fit its contents, eliminating the need for overflow hidden.

It looked like there was some unnecessary JavaScript code, so I simplified it to just toggle the visibility of the paragraph. Additionally, I added more semantic classes to the HTML paragraphs.

Below is the updated code snippet:

$(document).ready(function() {
  $('.downarrow').on("click", function() {
    $('.added-msg-content').slideToggle();
  });
});
.added-msg2 {
  padding: 3% 1%;
  float: left;
  width: 100%;
  box-sizing: border-box;
  font-size: 14px;
  color: #333333;
  position: relative;
  background-color: #e0e0e0;
}

.added-msg-inner {
  float: left;
  width: 100%;
}

.downarrow {
  position: absolute;
  right: 15px;
  bottom: -12px;
  z-index: 1;
  width: 30px;
  height: 30px;
  line-height: 30px;
  font-size: 18px;
  color: #fff;
  background-color: #003478;
  border-radius: 50%;
  text-align: center;
  font-weight: bold;
  cursor: pointer;
}

.added-msg-content {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="added-msg2">
  <div class="added-msg-inner">
    <p class="added-msg-author">Message added by agent user on<br> Sat, Jun 24th, 2017 (5:03AM)</p>
    <p class="added-msg-content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries</p>
    <p><i class="fa fa-paperclip" aria-hidden="true"></i> ABCFileName.pdf</p>
  </div>
  <div class="downarrow"><i class="fa fa-angle-down" aria-hidden="true"></i></div>
</div>

Answer №2

To display the paragraph after animation is completed, you can utilize the callback function of jQuery's animate() method instead of using the return statement:

$(document).ready(function() {
  // Storing inner height in a data property:
  $(".added-msg-inner").removeClass("added-msg-inner").each(function() {
    $(this).data({
      innerHeight: $(this).height()
    });
  }).addClass('added-msg-inner');
  $(".added-msg-inner > p:nth-child(2)").hide();
  $(".downarrow").click(function() {
    // Retrieving specific divView and innerHeight associated with this down arrow
    var $divView = $(this).siblings(".added-msg-inner");
    var innerHeight = $divView.data("innerHeight");

    $divView.animate({
      height: $divView.height() == 95 ? innerHeight : "95px"
    }, 500, function() {
      // Animation finished:
      if ($divView.height() > 95) {
        $(".added-msg-inner > p:nth-child(2)").show();
      } else {
        $(".added-msg-inner > p:nth-child(2)").hide();
      }
    });
    $('i', this).attr("class", $divView.height() == 95 ? "fa fa-angle-up" : "fa fa-angle-down");
  });
});
.added-msg2 {
  padding: 3% 1%;
  float: left;
  width: 100%;
  box-sizing: border-box;
  font-size: 14px;
  color: #333333;
  position: relative;
  background-color: #e0e0e0;
}

.added-msg-inner {
  float: left;
  width: 100%;
  height: 95px;
  overflow: hidden;
}

.downarrow {
  position: absolute;
  right: 15px;
  bottom: -12px;
  z-index: 1;
  width: 30px;
  height: 30px;
  line-height: 30px;
  font-size: 18px;
  color: #fff;
  background-color: #003478;
  border-radius: 50%;
  text-align: center;
  font-weight: bold;
  cursor: pointer;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="added-msg2">
  <div class="added-msg-inner">
    <p>Message added by agent user on<br> Sat, Jun 24th, 2017 (5:03AM)</p>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries</p>
    <p><i class="fa fa-paperclip" aria-hidden="true"></i> ABCFileName.pdf</p>
  </div>
  <div class="downarrow"><i class="fa fa-angle-down" aria-hidden="true"></i></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

Having trouble implementing showLoaderOnConfirm feature in SweetAlert2

I’ve been encountering some difficulties with implementing the showLoaderOnConfirm feature using sweetalert2 and ngSweetAlert for AngularJS. Although the code below runs smoothly and without any errors, I’m not seeing any loading animation. The alert ...

Displaying Image from Jquery Ajax Response

I have a PHP script that can resize images with specified dimensions by passing the width and height as variables like so: resize.php?width=100&height=200. The content-type of this PHP script is set to image/jpg. Additionally, I have an HTML page wher ...

Creating a peaceful web platform with React that supports several user roles

I am in the process of developing a single-page web application that must be completely restful, which is new territory for me. One challenge I'm facing is determining how to efficiently render the user interface for different roles using React. With ...

You are unable to select the radio buttons or checkboxes while they are located within the BootStrap

I am facing an issue with my bootstrap modal that includes radio buttons and checkboxes. When I place the code for these elements outside the modal, they work perfectly. However, once I move them inside the modal, I am unable to check or select them. I am ...

What is the method for obtaining the contents of innerHTML elements?

list.innerHTML = ` <p id="text" class="text">${toDoItem.task}</p> <div id="taskListBtn"> <span id="button-tick-${toDoItem.id}" class="material-icons tick-style">check_circle</span> <span id="button-edit- ...

Creating a JSON array using looping technique

I am attempting to construct a JSON array using a loop where the input className and value will serve as the JSON obj and key. However, I am facing difficulties in creating one and cannot seem to locate any resources on how to do so. Below is an example sn ...

Trigger a JavaScript function when a key is pressed down

Hey everyone, I'm trying to figure out how to trigger a JavaScript function when a particular key is pressed. For example, I want the function down() to be executed when the down key is pressed and function left() when the left key is pressed. Is ther ...

React Native buttons are displaying at a fixed width and not adjusting as expected

Within a row, I have two buttons with a padding of 20 between them, and they are only taking up the necessary width for the button text. The width remains constant! Here is the code for the buttons: <View style={styles.buttonContainer}> &l ...

involving mysql, php, and ajax for responsive navigation

I've successfully created a navigation system using MySQL and PHP. Now, I'm working on passing values to another page using AJAX. Can anyone lend a hand? <ul id="all_navs"> <?php $sql = mysql_query("select * from sub_category_mast ...

Reorganize the workflow in Node.js by moving the business logic from the controller to the

As I delve into learning Node.js, I've encountered a challenge with code refactoring. After researching the best practices for code architecture in Node.js, I am eager to refactor my code. The current state of my code: user.controller.js const bcry ...

Is there a way to achieve a similar outcome on my site?

Recently, I noticed a mouse-hover effect on two websites and found it quite appealing. https://i.sstatic.net/Ly0gP.png https://i.sstatic.net/oDe1i.png This is the specific effect that caught my attention. Can anyone provide me with insight on how to impl ...

aligning content that has exceeded the boundaries

I have a list of dynamically created <a> tags without text, to which I apply background images. These icons are displayed within a <div> container using the float: left; style. As the row of icons becomes too long, it overflows and starts a ne ...

After a mere 10 seconds, the cookie is still fresh and ready to indulge in

I recently created a cookie with the intention of having it expire after 10 seconds. Unfortunately, it seems to be persisting even after closing the browser. Below is the code I used to create the cookie. setcookie('attempt','', time( ...

Attempting to utilize the Optimist API's help() function to display the usage() information

I am relatively new to using optimist and despite my attempts at researching and experimenting, I have been unable to find a streamlined method for incorporating a --help option. In the documentation, there is mention of a help() function. Based on this i ...

Why is this loop in jQuery executing twice?

$(document).bind("ready", function(){ // Looping through each "construct" tag $('construct').each( alert('running'); function () { // Extracting data var jC2_events = $(this).html().spl ...

Guide on creating a dynamic slideshow viewer

I'm interested in learning how to implement a sliding image viewer using javascript or jquery. Based on what I've observed, these viewers feature images aligned side by side with buttons that shift the images depending on their width to display t ...

Direct jQuery to redirect to a new page and reveal a hidden div

I am facing a situation where I have two web pages named index and results. The results.html page contains some hidden divs with the CSS property set to display: none. My goal is to navigate from the index page using a navigation menu and open the results ...

Changing the background color with a switch function in ReactJS

After clicking a link, a view is rendered based on the "id" from a JSON. To enhance the user experience, I want to add a background color when a particular view renders and also toggle the style. This code snippet illustrates how the crawl is displaye ...

Django - issue with table display showing empty row due to query set

In my project, I have two models: one named Season and the other one named Game: # Season class Season(models.Model): teams = models.ManyToManyField('Team', related_name='season_teams', blank=True) current = models.BooleanF ...

Unable to set DIV to 'inline-block' or none based on checkbox selection

Despite reviewing multiple examples, I am still struggling to make this DIV visible and hidden by clicking a checkbox. Can someone please review my JavaScript code? <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Conten ...