Switching the class does not initiate the transition

I'm having trouble getting a transition to work when toggling a class. The class is being applied and the style changes accordingly, but the transition isn't triggering. I've checked that the transition applies to all properties, so I don't think that's the issue.

function expandFunction(element) {
  element.classList.toggle("setHeightStyle");
}
.setHeightStyle {
  height: 60px;
}

#expSect1 {
  overflow: hidden;
  cursor: pointer;
  transition: 1s;
}
<div class="setHeightStyle" id="expSect1" onclick="expandFunction(this)">
  <h1>Title</h1>
  <br>
  <p>Content!<br>More content!</p>
</div>

Answer №1

It is essential to correct not only the commas in the HTML code.

When using the transition, be sure to specify the property it will affect... If unsure, you can use all.

In order for the transition to work properly, the height must be defined within the #expSect1 rule... As transitions require a numerical value to begin with - using auto won't suffice.

Additionally, an id has higher specificity than a class... Meaning you should include both the class and the id in the rule that needs to be toggled.

Please refer to the functional example below.

function expandFunction(elementID) {
  let element = document.getElementById(elementID);
  element.classList.toggle("setHeightStyle");
}
#expSect1.setHeightStyle {
  height: 60px;
}

#expSect1 {
  overflow: hidden;
  cursor: pointer;
  height: 200px;
  transition: all 1s;
  background: blue;
}
<div class="setHeightStyle" id="expSect1" onclick="expandFunction(id)">
  <h1>Title</h1>
  <br>
  <p>Content!<br>More content!</p>
</div>

Answer №2

The issue lies in the fact that the height is not initially set on the element. To address this, I needed to adjust the selector for the added class in the HTML due to the specificity of your ID selector. It's generally recommended not to use IDs for styling purposes; they should be reserved for JavaScript functionality.

function toggleHeight(element) {
  element.classList.toggle("setHeightStyle");
}
#expSect1 {
  height: 200px;
  overflow: hidden;
  cursor: pointer;
  transition: all 1s;
}

#expSect1.setHeightStyle {
  height: 60px;
}
<div class="setHeightStyle" id="expSect1" onclick="toggleHeight(this)">
  <h1>Title</h1>
  <br>
  <p>Content!<br>More content!</p>
</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

Avoiding the use of apostrophes in jQuery or JavaScript

I need to show the text below for the <span> element. What's the best way to handle single quotes in this case? $("#spn_err").text($('#txt1').attr('value')+" is not valid"); The desired message format is 'ZZZ' is ...

Can Jest Vue handle loading dynamic imports for snapshot testing?

Having an issue with unit testing a Vue component that dynamically loads its child component. Jest and Vue utils don't seem to render it. Any suggestions on how to tackle this? Here's the component code: <template> <component :is=&quo ...

Triggering Actions with SVG Paths

In reference to the code snippet provided below on how to retrieve data with a click on an SVG path here, I am facing an issue with my example that displays Number 2. I want the alert button to appear only when the cursor is clicked near number 2. Currentl ...

Is there a way to retrieve two arrays in an Ajax request?

JAVASCRIPT CODE: $.ajax({ url: 'assignavailtrainers.php', data: {action:'test'}, type: 'post', success: function(data) { } }); PHP CODE: <?php $username = "trainerapp"; ...

What is the best way to utilize a single npm module in multiple TypeScript files?

Question: I keep encountering the error message "error TS2451: Cannot redeclare block-scoped variable 'os'" when I try to import the same npm module in multiple TypeScript files and run the TypeScript compiler tsc. Here is an overview of my proj ...

Efficient management of jQuery variables

I am currently working on a form and I have been trying to change an input's type using jQuery. Thanks to the helpful resources available on this website, I learned that I need to clone and replace the input in order to change its type. However, I enc ...

Vertically align elements within a bootstrap "card-body" with a fixed height inside a "card text-center" container

I need assistance with centering the h4 and button vertically on the page. Can someone help me achieve this? <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89ebe6 ...

There is a hidden delete button obscured by another element. What steps can be taken to bring that element to the forefront and make it visible?

I'm having trouble positioning a delete button in the top right corner of a collapsible box. Despite setting it to `visibility: visible`, I can't seem to get it to display at the top of the collapsible element. Any suggestions or ideas would be g ...

Transmitting data from the front end to the server in React/Shopify for updating API information using a PUT request

After successfully retrieving my API data, I am now tasked with updating it. Within my component, I have the ability to log the data using the following code snippet. In my application, there is an input field where I can enter a new name for my product an ...

Consistently maintaining a uniform distance between icons and the border box is essential

I am working on a team overview for a company where data such as name, job title, and information are fetched from the database. Due to varying lengths of information, the icons are not aligning properly in one line. https://i.sstatic.net/cEmKj.png Does ...

What steps do I need to take in order to create a histogram with perfect symmetry?

I am looking to create a unique JavaScript program that can generate a symmetric histogram resembling the image provided below: This program will prompt the user to input the number of bars they want to display and specify the character used to draw the b ...

I am seeking assistance with incorporating mySQL into my Node.js project with React

Currently, I am working on a web-app utilizing Node.js. The main goal is to retrieve information from my local database in MySQL Workbench. Initially, I decided to focus on building the frontend interface before diving into implementing the functionality s ...

Creating Chaos in PrimeFaces Page Navigation with JavaScript

I have implemented third-party connection monitoring code called Online JS by Denis Radin and it seems to be causing unexpected interactions with the navigation elements on my webpage. I am seeking insight into possible reasons for this behavior in order t ...

Tips for adjusting the alignment of numbers in an ordered list to the right

Check out the code snippet below: The list items are correctly aligned to the right, but the numbers are not. How can we adjust the CSS so that the numbers align properly on the right as well? ol { width: 15vw; } <ol style="text-align:right;"> ...

The issue arises with Phonegap/Cordova socket plugins resulting in an error message: "undefined is not a function in cordova.js"

I've integrated a phonegap/cordova plugin for sockets on Android, but I'm facing issues getting it to work. The plugin can be found here. Upon checking the log messages "debug 1" and "debug2", it seems that creating the socketHandle object is su ...

Modifying the app.css file in the source tab of dev tools does not cause any changes in the DOM when working with a

Just starting out with react js here. While exploring developer tools in Chrome, I attempted to tweak some CSS in the elements panel and noticed the changes reflecting above in the DOM. However, when I navigate to the sources tab, I'm unable to modify ...

JSON payload with an array that includes nested JSON objects

I am struggling with JSON objects and need to create a JSN structure similar to the following: { name: 'root', children: [ name: 'son1', children: [....] ] } Could someone please guide me on how to construct it using Java ...

How can Vuetify be used to create a v-row that floats above other elements, maintains a fixed position, and still adheres to the width of its parent element

Currently, I am integrating Vuetify into my project and facing an issue with making an element inside a v-row float and fixed while still maintaining the width of the parent element. Here's the code snippet I am working with: <v-container> ...

Employing PHP to iterate through and separate items into disparate divs

For my project, I need to loop through 12 items and separate them into different divs. Specifically, I want to group 0 and 1 together in one div, have 2 in its own div, 3 and 4 in another div, 5 in a separate div, and so on. <!-- Grouping 0 and 1 --> ...

Direct your cursor to move across the sphere surface in THREE.JS

I'm working on a project to develop an interactive globe where users can drag points on the surface using their mouse cursor. Here is the code snippet I've been working on Currently, I've managed to set up the main sphere and a smaller red ...