Unusual activity discovered when using JavaScript addClass and removeClass methods

While experimenting with animate.css, I created a custom Javascript function to initiate animation events.

This simple function, triggered by an onClick event on a button, is only three (or four) lines long, but encounters a perplexing error.

The anim function removes the class from the element and then adds it back in, allowing for multiple button presses to trigger the animation. However, I encountered a problem where the animation would only run once and fail to repeat upon subsequent clicks.

In a surprising turn of events, I found that adding console.log(element_name) in line 3 magically made it work.

I was baffled by this behavior and attempted to add other console.log statements, but strangely only console.log(element_name) had any effect!

To confirm that this wasn't just a glitch in my development environment, I replicated the issue on JSFiddle here.

function anim(element_name){ //'animate' is a reserved keyword
    removeClass(element_name, 'bounceInDown');
    console.log(element_name); //Very cute behaviour!!! when I put this line here, it works, if i don't it doesnt.
    //console.log('a'); //Not all console.logs work
    addClass(element_name, 'bounceInDown');
}

function addClass(element, classname){
    element.classList.add(classname);
}

function removeClass(element, classname){
    element.classList.remove(classname);
}

Answer №1

The reason behind this issue is that JavaScript compilers attempt to execute multiple actions simultaneously, preventing your class from being deleted. To resolve this, you can trigger a reflow of the page using the following method:

document.body.clientHeight;

By implementing this change, the problem should be resolved!

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

The ASP.NET session encounters difficulties in refreshing itself

I have been given the task of enhancing a web application built on ASP.NET 4.0 Web Forms to alert users before their session expires, and give them the option to either continue the session or end it. To achieve this, I have implemented a confirmation dia ...

Selecting default values from the Vuex store in Vue-Multiselect

Picture a Vuejs-Application equipped with Vuex. I am aiming to incorporate a mulitselect-dropdown. Here's a basic example inside the Vue-component: <template> <div> <multiselect v-model="values" :options="options"> ...

Angular2 Animation for basic hover effect, no need for any state change

Can anyone assist me with ng2 animate? I am looking to create a simple hover effect based on the code snippet below: @Component({ selector: 'category', template : require('./category.component.html'), styleUrls: ['./ca ...

What is the best way to ensure that my <h1> header stays above my <h2> header at all times?

As a novice in web design, I recently took on the challenge of creating my own website. I am a graphic designer and came up with a mock-up of how I envision the site to look. While I managed to complete most parts of it, there's one specific area wher ...

Problems with select tag change events

I encountered an issue with select tag onChange events. When I select a value from the select tag, it should display in a textbox that is declared in the script. It works perfectly when I remove the class "input" from the select tag, but I prefer not to re ...

What is the best method to reset values in ngx-bootstrap date picker?

At the moment, it is only accepting the most recently selected values. To see a live demo, click here. ...

Can you suggest a way to revise this in order to include the type of property (string)?

Here is a snippet of working code that raises a question about refactoring to improve the readability and functionality. Consider renaming the method to isPropValueSame. import * as _ from 'lodash'; const diff = _.differenceWith(sourceList, comp ...

Choose from a range of knockout fetching options for the select feature and choose one

Upon loading my page, there is a <select> element positioned. I am utilizing knockout data-bind to retrieve the values like this: <select data-bind="attr: { id: 'input-' + id(), required: !allowBlank }, option ...

What is the best way to retrieve all Objects by a specific property in React when working with an array of Objects?

I am looking to create a multiple checkbox filter where selecting the 'Accessories' option will display items with the 'Accessories' value under the 'type' property. However, before I proceed with that, I need to ensure that I ...

Learn how to utilize ajax and jquery to fetch data from an external database without the need for refreshing the page

I'm facing an issue with a small script. I want to query the Content in the database on a simple HTML page without any page refresh. <form method="get" name="formrrv" id="formrrv"> <input type="hidden" name="calculate" value="1 ...

Position an iframe in alignment

I'm working on a HTML page and I have been trying to figure out how to align an iframe at the bottom of the page so that it spans the entire width. Despite my efforts, I haven't been successful in getting the iframe to align properly at the botto ...

Questions about syntax: What is the correct course of action?

If there is a child inside a div with an id, such as id="mother", how should the css be written correctly? Example: 1) #mother ul li {...} or 2) .mother ul li {...} Is there a difference? The second example I came across when MOTHER had a class name, ...

Node.js: Module not found error

When I run the command below to install a module from the NPM registry: npm install dc All files are successfully installed, but upon running the script dc, it fails to resolve a dependency. $ node web-test.js module.js:340 throw err; ^ Error: ...

The data retrieval process in the Node JS API was unsuccessful

Recently, I created an API using express and MongoDB, it functions well with Postman but encounters issues when incorporated into my Next JS app using the fetch() method. GET requests work without a hitch, however, POST requests are problematic. Despite ad ...

Namespacing is not applied to dynamic modules in Vuex

I've been tackling a modular vue application that enrolls the modules during compile time. Take a look at the code snippet below - app.js import store from './vue-components/store'; var components = { erp_inventory: true, erp_purc ...

Express is capable of dynamically modifying routes at runtime

I've set up an express server with my index.js looking like this: let some_parameter = some_value; const configuredHandler = new Handler(some_parameter); const server = express(); server .get("*", configuredHandler.handleRequest) .post("*", ...

The icons at the bottom of the page are not properly aligned

I am having trouble aligning my footer icons, specifically the first 2 images. While the bootstrap icons are centered correctly, the first 2 images are not aligning as expected. I have tried various methods like align-items-center, justify-content-center, ...

What is the most effective way to transform THREE.Geometry into an ArrayBuffer, File, or Blob?

I am looking to optimize my code by transferring a THREE.Geometry object to an HTML5 Web Worker without serializing it for performance reasons. I am interested in converting it to a Transferable Object such as an ArrayBuffer, File, or Blob to pass it by ...

My pure JS component is not being recognized by ASP.NET Core when integrated with Vue.js

I decided to try writing a simple component in "ASP.NET Core with Vue.js" template using pure JS instead of Typescript. However, I encountered an issue where my port does not seem to work properly. What could be causing this problem? in ./ClientApp/compon ...

New Feature in Bootstrap4: Navigation Bar Alignment Shift to the Left

I need help aligning my links to the right side of the page I attempted to use mr-auto but it was not effective <body> <nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top mr-auto mt-2 mt-lg-0"> <a class="navbar-brand" hre ...