Ways to dynamically display or conceal a button based on the amount of data in a div

I need help adjusting the toggle button in my code. Currently, it works well without the 'if' condition, but I want it to hide when there is less data and appear when there is more data. Additionally, I want to set a minimum height of 100px for the content div. Could someone assist me with this? Thank you.

https://i.sstatic.net/T2X09.pnghttps://i.sstatic.net/lQF7E.pnghttps://i.sstatic.net/pBYrn.png

var elmnt = document.querySelector(".backwhite");
var txt = elmnt.clientHeight + "px";
if (txt >= 100 + "px") {
var mydivh = document.querySelector(".backwhite");
mydivh.style.height = "100px";

function toggleDescriptionHeight(e) {
document.querySelector(".backwhite").classList.toggle('expanded');
e.target.textContent == 'Expand' ? e.target.textContent = 'Collapse' : e.target.textContent = 'Expand';
}
var button = document.querySelector('.btn');
button.addEventListener('click', toggleDescriptionHeight)
} else {

var myElements = document.querySelector(".bbttnn");
myElements.style.display = "none";
var myElement = document.querySelector(".backwhite");
myElement.style.height = "100px";
}
body {
background-color: #f1f3f6;
}
.backwhite {
background-color: #fff;
padding: 15px;
overflow: hidden;
}
.backwhite.expanded {
height: auto;
}
<div class="container">
<h4>Description</h4>
<div class="backwhite">
<p>1. Create Ui For Email Campaign.</p>
<p>2. Create Functionality of Email Campaign</p>
<p>3. Create Keyword Display using Drag And Drop Functionality.</p>
<p>1. Create Ui For Email Campaign.</p>
<p>2. Create Functionality of Email Campaign</p>
<p>3. Create Keyword Display using Drag And Drop Functionality.</p>
<p>1. Create Ui For Email Campaign.</p>
<p>2. Create Functionality of Email Campaign</p>
<p>3. Create Keyword Display using Drag And Drop Functionality.</p>-->
</div>
<button type="button" class="btn btn-default btn-block">Expand</button>
</div>

Answer №1

Multiple redefinitions of variables were consolidated, CSS adjustments were made to accommodate all height definitions, and the if statement was cleaned up to remove 'px' (which can affect the comparison calculations).

It's important to note that if the minimum height in the CSS is set to 100px, the JavaScript code may not successfully hide the button. To address this, an additional count of child divs was implemented to enable the button hiding functionality when fewer than 3 are present.

The code has been rearranged and annotated for clarity in the following snippet.

var elmnt = document.querySelector(".backwhite"),
  txt = elmnt.clientHeight,
  button = document.querySelector('.btn');
// defining elmnt once is sufficient

function toggleDescriptionHeight(e) {
    elmnt.classList.toggle('expanded');
    if (e.target.textContent === 'Expand') {
      e.target.textContent = 'Collapse';
    } else {
      e.target.textContent = 'Expand';
    }
} // the function has been moved outside the if condition

if (txt >= 100 && elmnt.children.length > 3) { // the 'txt >= 100 &&' condition simplified
  button.addEventListener('click', toggleDescriptionHeight);
} else {
  button.style.display = "none";
}
body {
  background-color: #f1f3f6;
}
.backwhite {
  background-color: #fff;
  padding: 15px;
  height: 100px;
  /* height defined here */
  /* min-height: 100px; */
  overflow: hidden;
}
/* ensure button visibility with minimum height of 100px */

.expanded {
  /* should come after .backwhite */
  height: auto;
}
<div class="container">
  <h4>Description</h4>
  <div class="backwhite">
    <p>1. Create Ui For Email Campaign.</p>
    <p>2. Create Functionality of Email Campaign</p>
    <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
    <p>1. Create Ui For Email Campaign.</p>
    <p>2. Create Functionality of Email Campaign</p>
    <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
    <p>1. Create Ui For Email Campaign.</p>
    <p>2. Create Functionality of Email Campaign</p>
    <p>3. Create Keyword Display using Drag And Drop Functionality.</p>
  </div>
  <button type="button" class="btn btn-default btn-block">Expand</button>
</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

Discover the Magic of Jquery Hover Effect Unleashed

$('.outerBox').hover( function() { $(this) > $('.innerBox').stop(); $(this) > $('.innerBox').slideDown(750); }, function() { $(this) > $('.innerBox').stop(); $(this) > $(&apos ...

Enhance User Experience with a Responsive Website Dropdown Menu

Currently, I am focused on enhancing the responsiveness of my website and I realized that having a well-designed menu for mobile view is essential. To address this need, I added a button that only appears when the screen size is 480px or lower, which seems ...

Top method for verifying input during keyup or blur events

When it comes to validating user inputs, I often find myself wondering about the best approach to take. In this case, I have created a regex for numbers with decimal points. .ts part checkIsNumber(event) { console.log('event', event.target. ...

How to retrieve a specific object within a specific container using jQuery

Consider the layout presented below: <span class="container"> <video class="video"> <source ... /> </video> <div class="controls"> <div class="play">Play</div> </div> </spa ...

Navigating through intricate JavaScript object

I need help figuring out how to access the "description" property of a JSON object I received from an API endpoint. The object looks like this: - "description" : "Dorian Market 1"? let markets = { "result":[ { "townId" : "MEBD", "stor ...

Is there a way to verify the content inside the :before selector using Jasmine within an Angular 6 application?

Looking to test whether the content of a label changes based on the checkbox being checked using Jasmine in an Angular 6 project. Is this feasible? Code snippet: HTML <input id="myCheck" class="checkbox" type="checkbox" /> <label for="myCheck" ...

Transform incoming AJAX response into Backbone model

Is there a way to transform the success callback data into a Backbone model? Here is what I currently have: App.Models.Image = Backbone.Model.extend({ idAttribute : 'image_id' }); App.Collections.Image = Backbone.Collection.extend({ model : ...

Retrieving a compilation of items found within text selected by the user on a website

Imagine a scenario in which a webpage contains the following structure: <p> <span class="1">Here's some text</span> <span class="2">that the user</span> <span class="3">could select.</span> </p> I ...

Rendering Local HTML with local styling and scripts in React Native Webview

I am having trouble loading a local HTML, CSS, and JS file in my React Native webview. The CSS is not being applied to the HTML. Here is my folder structure: https://i.sstatic.net/mqYT9.png My HTML file, along with CSS, images, and JS file are all placed ...

Angular 2 doesn't reflect changes in component variables in the view until mouseover happens

Since updating from angular2-alpha to the latest version, I've noticed that when a boolean value changes in my *ngIf directive, it doesn't reflect in the view until certain actions are taken. Here is the specific component code: declare var CKE ...

`Is there a way to choose several radio buttons with varying names using just one label?`

Is there a way to choose multiple radio buttons with different names using just one label? Here's an example of what I'm attempting to accomplish... <input id="1A" type="radio" name="first" value="A">firstA<br> <input id="1B" typ ...

The addition of special characters to strings in TypeScript through JavaScript is not functioning as expected

I need assistance on conditionally appending a string based on values from captured DOM elements. When the value is empty, I want to include the special character "¬". However, when I try adding it, I get instead because the special character is not reco ...

Error: The 'inputValue' property in the Select component of antd is malfunctioning

Currently, I am utilizing the antd library's Select component (version v3.23.1) with the mode="multiple" setting. I have observed that when I type in the search field within the Select component and then click outside of it, the searched text gets cle ...

npm will only update when modifications are made to index.js

Recently diving into the world of React, I've encountered an issue with updating my website when making changes to files within my project. While modifications to the index.js file reflect on the site, any adjustments made to imported apps do not get ...

Customize the background color of a clipPath in SVG

I'm looking to design a landing page featuring a colored div with a clip-path, intended to showcase a video playing in the background. However, I'm encountering an issue where the background looks grayish without any clear explanation. Despite t ...

What is the best way to align various types of content within a div container

I'm struggling with aligning a label next to a checkbox in my HTML code using Vue.js. Here's how it looks: <div style="margin-bottom: 15px;"> <input id="newsletterRegister" type="checkbo ...

Display or conceal checkboxes according to the elements in an array

After just starting to delve into Angular and javascript, I've been racking my brain for the past couple of days. I have this table in html where I need to display checkboxes based on certain conditions. Each row consists of 5 checkboxes, and what is ...

What causes the inconsistency in CSS Hover behavior?

Looking to devise a button in css and html that reveals another button below it by rotating on its lowermost axis when hovered over. Progress has been made here: http://codepen.io/machinarius/pen/BdtCb However, there seems to be an issue with the hover b ...

Retrieve the $first property from ng-repeat within the controller

I created a table with two input boxes, and by utilizing ng-repeat I have looped through the values: <tr ng-repeat = "row in allRows" > <td> <input type="text" ng-model="row.name" ng-readonly="toogleRN && $first"/> </td> & ...

What is causing my column's content to be lacking padding on the left side and having excessive padding on the right side?

Edit: Here's the link to the codepen https://codepen.io/maptik/pen/bGKMgpJ In my React project, I'm utilizing Bootstrap to display cards for each "Product". Here is a snippet of how I am rendering it: <div className="container bg-color- ...