Changing in height by utilizing javascript's style.height animation

When attempting to adjust the height property using JavaScript instead of jQuery, a challenge arises. The issue lies in the inability to reset the height back to zero after setting it to the scrollHeight of the element.

The following is the JavaScript code being used:

let isClosed = true;
var cals = document.getElementsByClassName('h-strench-box');

for (var i = 0; i < cals.length; i ++) {
   cals[i].addEventListener('click', function(e) {
       if (isClosed) {
           this.classList.add('h-strench-box-out');
           var content = this.querySelector('.h-strench-content');
           content.style.height = content.scrollHeight + 'px';
           isClosed = false;
       } else {
           if (this.classList.contains('h-strench-box-out')) {
               this.classList.remove('h-strench-box-out');

               // this.querySelector('.h-strench-content').style.height = '0';
               // This not working
               isClosed = true;
           } else {
               for (var j = 0; j < cals.length; j ++) {
               cals[j].classList.remove('h-strench-box-out');
               cals[j].querySelector('.h-strench-content').style.height = '0';
               // This not working
               }
           this.classList.add('h-strench-box-out');
           var content = this.querySelector('.h-strench-content');
           content.style.height = content.scrollHeight + 'px';
           }
        }
                        
    });
}

CSS styles:

.h-strench-content {
    height: 0;
    padding: 0;
    display: none;
    overflow: hidden;
    transition: height 0.4s ease-in;
}
.h-strench-box-out .h-strench-content {
    display: block;
}
.h-strench-btn {
    transition: transform 0.3s ease-out;
}
.h-strench-btn::before {
    content: '\f13a';
    font-family: "Font Awesome 5 Free";
    font-size: 27px;
    font-weight: 600;
}

An additional question is how to modify the height value(B) without using element.style (A). See the image linked below for comparison. Any assistance would be greatly appreciated!

https://i.sstatic.net/4BMAK.png

Answer №1

One of the initial steps is to ensure that your querySelector calls are returning the correct elements. It will always fetch the first element that matches the specified selector.

Regarding the CSS: Height A is defined inline, giving it priority over height B unless height B is marked as !important. To switch back to height B, height A needs to be completely removed using remove property or simply set to null.

var target = document.getElementById('name');
target.style.removeProperty('height');
// if you wish to retrieve the previous value...
// var oldValue = target.style.removeProperty('height');

If you aim to modify content within a stylesheet, consider this example:

var declaration = document.styleSheets[0].rules[0].style;
var oldValue = declaration.removeProperty('height');

... Nevertheless, exercise caution when utilizing the stylesheet example, as changes in indices can disrupt its functionality. It would be more secure to explore alternatives that add or remove classes with the desired values instead.

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

React search filter feature with dropdown selection

After successfully implementing a search filter in my React app, I encountered an issue when trying to incorporate a select tag to change the search filter criteria. class BookList extends Component { state = { search: '', selectedValue: ' ...

What is the process for obtaining the cypress interception fixture twice but in two distinct formats?

Hi there, I'm new to Cypress and facing a challenge that I hope you can help me with. In my test file, I have the following code: cy.SetupClassificationsStubFixture(1); This code refers to a custom command that I've created: Cypress.Commands.ad ...

Is there a way to determine the app bar height within a React Material UI application?

I'm trying to create a full-screen image for the opening of my website using React and Material UI. Here's a snippet of my JSX code with the default Material UI theme: <AppBar> //code in between </AppBar> <Container sx={{margin: ...

What is the best way to incorporate a description box for each city on the svg map that appears when you hover your mouse over it?

I am looking to display detailed descriptions for each city in the same consistent location on my map. With multiple pieces of information to include for each city, I want to ensure that the description box is positioned at the bottom of the map. Can any ...

What methods can be used to limit URL abbreviations on my website?

I need a simple solution that I have been struggling to figure out. What I want is for anyone attempting to access any webpage directly on my website to be automatically redirected to the home page. For instance, if someone tries to access www.domain-name ...

What is the best approach to simultaneously update an array using multiple threads in a Node.js environment?

Hey there, I'm trying to figure out how to make changes to the same array using 2 worker threads in Node.js. Can anyone help me with this? My issue is that when I add a value in worker thread 1 and then try to access it in worker thread 2, the second ...

Adapting my JavaScript code to handle one-dimensional CSV data instead of the usual two-dimensional format

How can I effectively parse this CSV file using JavaScript? 1363085391,42.890000000000,5.432200000000 1363088879,47.570000000000,4.981800000000 1363120475,56.560000000000,1.768000000000 1363132522,53.000000000000,1.000000000000 1363214378,48.630000000000, ...

What is the best way to set up the login page and logged-in homepage using Node, Express, and Passport with the "/" route?

I am currently in the process of developing an application using Node.js, Express.js, and Passport.js. My goal is to create a seamless user experience on the homepage where if you are not logged in, you will see a login form (with potential for additional ...

Ensuring maximum length validation on input

I am having an issue with the function "checkNumbers()". I am attempting to validate if my input does not exceed 10 numbers; while "isAllowedSymbol()" is functioning correctly, the other one is not. What could be causing this problem? function isAllowe ...

Middle-Click JavaScript Action

On the website I'm currently working on, there is a button that uses a sprite sheet animation and therefore needs to be set as a background image. I want the button to have a slight delay when clicked, so the animation can play before redirecting to a ...

My goal is to prevent users from using the Backspace key within the input field

Let's say we want to prevent users from using the backspace key on an input field in this scenario. In our template, we pass the $event like so: <input (input)="onInput($event)"> Meanwhile, in our app.component.ts file, the function ...

The agony of CSS in Emacs on Auto Complete Mode, portrayed vividly!

Recently, I added Auto Complete Mode to my Emacs setup. Issue #1: When typing declarations, the auto-complete feature works fine until I hit ;: Unexpectedly, it automatically tries to complete something! Pressing Enter would accept this unwanted completi ...

Learning the ins and outs of Node.js: Creating a client to connect to a Node.js server and receive broadcast messages

In implementing my nodeJS Server, everything seems to be running smoothly. However, now I am looking to create a client that can receive messages from the server and trigger specific JavaScript functions based on those messages. The process involves: Us ...

Bypass the Array.reduce method in JavaScript

I'm currently working on finding an elegant solution to a toy example pattern. The goal is to stop the reduce algorithm immediately and return 0 when an element with a value of 0 is encountered, without processing the rest of the elements. let factor ...

Tips for delaying my response in nodejs until the loop is complete

This is the code I'm currently working with: myinsts.forEach(function (myinstId) { Organization.getOrgById(myinstId,function (err, insts) { res.json(insts); }) }); I am using Node.js and encountering an error message that says "Can't set hea ...

Arrange Drifting Components in a Fluid Layout Grid

I am in the process of creating a unique sticky note website where I want to arrange the notes in a grid format. As the window size decreases, the notes should automatically reorganize to fit the width and allow for vertical scrolling. My goal is to achiev ...

Troubles with Angular elements not aligning correctly when using the "display: block" property

When using an angular element for a directive with "display: block", it may not automatically take up 100% of the width of the parent container. In order to ensure that it does, the width must be explicitly set to "100%" in the CSS. Despite setting "width: ...

Issues with Google Fonts failing to load correctly

Would you mind taking a look at this issue for me? In all browsers except Firefox, the expected behavior is that a web font remains invisible until fully loaded, preserving space on the page. Interestingly, in Chrome and IE9 (the browsers I tested), ther ...

A guide on eliminating null or empty values within a React table's map

Is there a way to determine if the value of {row.storeId} is null or empty? If it is, I would like to display NA within the <TableCell>. {props.tableData.map(row => ( <TableRow key={row.storeId}> <TableCell>{row ...

What is the process for loading this image?

While browsing through this stunning website, I came across a feature that caught my eye. Can someone please explain how the designer displayed the "The magic is loading" effect: The image vanishes once the site has finished loading completely. ...