Struggling to restart CSS3 animation following an Ajax response

Below is an example of an HTML form:

<form method="post" action="camount.php" id="loginForm">
  <span id="heading">
    Username: <input type="text" name='us' id='us'/><br />
    Password: <input type="password" name='pa' id='pa'/><br />
  </span>
  <input type="button" value="Sign in" onclick="isPasswordCorrect(document.getElementById('us'), document.getElementById('pa'))" /><br />
  <span class="animated shake" id="report"></span>
</form>

This snippet of JavaScript code is what triggers the function mentioned above

if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
  if(xmlhttp.responseText)
    document.getElementById("loginForm").submit()
  else{ 
    document.getElementById("report").style.webkitAnimationName = "";
    setTimeout(function (){
    document.getElementById("report").style.webkitAnimationName="animated shake";
    }, 0);
    var element = document.getElementById('report');
    element.innerHTML = "wrong password/username"   
    password.value = ""
  }
}
xmlhttp.open("post", "CheckCred.php", true)
//required for sending data through POST
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlhttp.send("name="+encodeURIComponent(name.value)+
             "&password="+encodeURIComponent(password.value))

The following CSS should make the text inside the <span> tag turn red and shake. While it does become red, the shaking effect doesn't seem to work.

.animated{
  -webkit-animation-fill-mode:both;
  -moz-animation-fill-mode:both;
  -ms-animation-fill-mode:both;
  -o-animation-fill-mode:both;
  animation-fill-mode:both;
  -webkit-animation-duration:1s;
  -moz-animation-duration:1s;
  -ms-animation-duration:1s;
  -o-animation-duration:1s;
  animation-duration:1s;
}
.animated.hinge{
  -webkit-animation-duration:2s;
  -moz-animation-duration:2s;
  -ms-animation-duration:2s;
  -o-animation-duration:2s;
  animation-duration:2s;
}
@-webkit-keyframes shake {
  0%, 100% {-webkit-transform: translateX(0);}
  10%, 30%, 50%, 70%, 90% {-webkit-transform: translateX(-10px);}
  20%, 40%, 60%, 80% {-webkit-transform: translateX(10px);}
}
@-moz-keyframes shake{
  0%, 100% {-moz-transform: translateX(0);}
  10%, 30%, 50%, 70%, 90% {-moz-transform: translateX(-10px);}
  20%, 40%, 60%, 80% {-moz-transform: translateX(10px);}
}
@-o-keyframes shake{
  0%, 100% {-o-transform: translateX(0);}
  10%, 30%, 50%, 70%, 90% {-o-transform: translateX(-10px);}
  20%, 40%, 60%, 80% {-o-transform: translateX(10px);}
}
@keyframes shake{
  0%, 100% {transform: translateX(0);}
  10%, 30%, 50%, 70%, 90% {transform: translateX(-10px);}
  20%, 40%, 60%, 80% {transform: translateX(10px);}
}    
.shake {
  -webkit-animation-name: shake;
  -moz-animation-name: shake;
  -o-animation-name: shake;
  animation-name: shake;
}    
span#report{
    display: block;
    color: #F80000;
}

I've attempted to troubleshoot based on guidance from this source but have yet to succeed. I am specifically aiming for this functionality to be operational in FireFox. Any insights on what might be incorrect or why the text "wrong username/password" isn't shaking would be greatly appreciated.

Following a suggestion from MarZab, I also tested out

document.getElementById("report").style.webkitAnimationName = "";
setTimeout(function (){
  document.getElementById("report").style.webkitAnimationName = "";
  document.getElementById("report").style.webkitAnimationName = "animated shake";
}, 4);

However, the desired shaking effect still fails to materialize.

Answer №1

Opt for className over webkitAnimationName

Take a look at this example on jsFiddle

During our discussion in the chat, we identified that the main issue was related to the execution line

Browsers usually update the DOM state after executing code. Thus, if the class remains unchanged within the same execution context, the animation will not be triggered.

By placing the unset action outside of the current execution line, it prompts the DOM to update accordingly.

The corrected code snippet is as follows:

function isPasswordCorrect(name, password) 
{ 
  report.className = ""; 

  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP") 

  xmlhttp.onreadystatechange=function() 
  { 
    report = document.getElementById('report'); 
    report.className = "animated shake"; 
  } 
}

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

Utilizing Express JS: Invoking a function within my class during routing operations

Currently, I am in the process of developing a MERN Application using Typescript. I seem to be encountering an issue with this within my class. When utilizing this code snippet: router.post("/create", user.createNewUser); It becomes impossible ...

Tips on choosing a button and applying custom styles with emotion styles in MUI

Looking to apply a margin-right to my button. Currently utilizing mui 5 with Button variant='contained'. I created a custom CSS style using the styled component in mui and targeted the Box. const Wrapper = styled(Box)({ display: 'flex&ap ...

Take me to a different page through colorbox

Currently, I am facing an issue with my colorbox. My objective is to redirect to another page once a certain value has been validated using PHP. Below is the code snippet that I have attempted: if (isset($_POST['confirm'])) { echo "<scrip ...

Tips for extracting information from Firebase and showing it on a Google gauge chart

I'm having some trouble displaying data from Firebase on a gauge chart. I keep getting an error that says "Uncaught (in promise)". Here's the JavaScript code I've been working with: <script type="text/JavaScript"> google.ch ...

Tips for implementing a sticky sidebar in HTML5 with jQuery

I currently have two files: index.php and aside.php. I've already created the contents of aside.php. Now, I want to make the aside page sticky. To achieve this, I referred to the following link: http://codepen.io/pouretrebelle/pen/yvcBz My struggle ...

Automatically load file and play audio when the page is loaded

I have created code that allows me to input an mp3 file, output the audio, and display a visual representation of the music. Currently, loading the file is done manually through an input type="file". Now, I want to change it so that the music automaticall ...

Toggle between a list view and grid view for viewing photos in a gallery with a

Hey there, I'm a newbie on this site and still getting the hang of jQuery and JavaScript. Though I do have a good grasp on HTML and CSS. Currently, I'm working on a photo gallery webpage as part of my school project using the Shadowbox plugin. Wh ...

Looking for an easy solution for bundling and vendoring NPM packages?

Full disclosure: My expertise lies mainly in backend development, and I don't have extensive knowledge in front-end technologies. I am in the process of creating a Golang web application that does not use a single page application (SPA) architecture. ...

I often struggle with creating social media buttons using Material UI

https://i.sstatic.net/jmZKS.jpg https://i.sstatic.net/4RQwe.jpg Unfortunately, the code provided is not rendering social media icons or buttons as expected. In the initial lines, material UI icons/buttons were imported from the React Material UI library ...

Firefox with Ajax bug bar

I have encountered an issue with my Navbar specifically on Firefox. The problem I am facing is that the Navbar is flickering, constantly growing and shrinking. Interestingly, this issue does not appear when using Chrome or Opera. I'm curious to know ...

Is it possible to deploy a build across various website subdomains without altering user data?

Currently, I am in the midst of developing a project for a client where I am responsible for both the front end and back end components. After much consideration, I have opted to use Remix due to my familiarity with React and proficiency in JavaScript/Type ...

Getting systemjs to load a module from a CDN in global format: The ultimate guide

I keep encountering the following error: EXCEPTION: ReferenceError: cast is not defined in [null] To create a custom Chromecast receiver application, you need to utilize a specific js file that exposes the necessary functionality through a global 'ca ...

Tips on skipping the need to repeatedly use `@ApiProperty()` for every dto in NestJs-swagger

I'm currently exploring ways to streamline the process of specifying @ApiProperty() for each DTO. I've heard about a method involving the creation of a nest-cli.json file, where if you define Promise<DTO> in your controller within nest-swa ...

Copying a class and adding the second item in the duplicated class

I have been working with an ajax function to retrieve names from the database. The issue arises when there are multiple names returned - I split them and then attempt to clone the first class in order to display the additional names. However, this proces ...

Is there a way to enclose a set of elements that are aligned to the

I am facing an issue with a container div that contains multiple items. The container requires a border, but the problem arises when I float the elements left within the container, as it seems to disrupt the flow of the elements. Adding a border to the co ...

Having trouble with creating a new Next.js app using the latest version with npx?

Having some difficulty with the "npx create-next-app@latest" command while setting up Next.js. As a newcomer to both the community and Next.js, I could use some assistance in resolving this problem. Upon running the command, an unfamiliar message was displ ...

Utilizing AngularJS: Binding stateParams value to custom data within state objects

Following the guidelines here, I am setting a page title in my state object. $stateProvider .state('project', { url: '/projects/:origin/:owner/:name', template: '<project></project>', data : { pageTi ...

Interacting with Command Line in NodeJS using Express

Is there a way to connect with the JS environment of an Express app through the command line, just like using a browser's console? For instance, let's assume Express is up and running. Can I inspect variables by logging them to the terminal usin ...

What is the most efficient method for transforming an index into a column number that resembles that of Excel using a functional approach?

Is there a way to write a function that can produce the correct column name for a given number, like A for 1 or AA for 127? I know this question has been addressed numerous times before, however, I am interested in approaching it from a functional perspect ...

Encountering an issue while retrieving data from my personal server: "Error message states 'Unexpected end of JSON input'."

As a beginner in Backend development, I decided to experiment with API calls and Client-Server interactions. const express = require("express"); const cors = require("cors"); const fetch = require("node-fetch"); const app = e ...