New jQuery div elements do not have animations when using $(document).on

After creating an animation function that worked well with hovering over squares and leaving a trail, I later needed to add or remove squares based on the page size. Seeking help from here, I discovered my bind animation function wouldn't work on new divs.

Upon researching and trying different functions, I modified the code to look like this:

$(document).ready(function(){
    $(document).on("webkitAnimationEnd mozAnimationEnd animationend", ".square", function(){
      $(this).removeClass("squareAnim");
    });
    $(document).on("hover", ".square", function(){
      $(this).addClass("squareAnim");
    });
});

Unfortunately, the modified code is not functioning as expected. I am unsure if $(document).ready is redundant in this case, but even removing it does not solve the issue. What could be missing?

For reference, here is the link to the jsfiddle.

Answer №1

Remember to bind the events again when creating new elements (e.g., onresize). Simply binding them once on page load with onready will not work for newly added elements.

Also, note that onhover is no longer supported (removed in jQuery 1.9) and should be replaced with onmouseenter and onmouseleave.

According to jQuery's documentation:

Deprecated in jQuery 1.8 and removed in 1.9: The name "hover" was used as a shorthand for "mouseenter mouseleave". It assigned a single event handler for both events, and the handler had to check event.type to determine if it was mouseenter or mouseleave. Do not confuse the "hover" pseudo-event-name with the .hover() method, which can accept one or two functions.

$(window).on("resize", function() {
  multiplyNode(contain.querySelector('.square'), canAdd(), false);
  $(document).on("webkitAnimationEnd mozAnimationEnd animationend", ".square", function() {
    $(this).removeClass("squareAnim");
  });
  $(document).on("mouseenter", ".square", function() {
    $(this).addClass("squareAnim");
  });
}).resize();

View updated fiddle

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

Unable to verify POST $http request using $httpBackend

Currently, I am in the process of writing unit tests to cover the http requests of my app. Using Jasmine and Karma, I have been following a tutorial on how to use $httpBackend from https://docs.angularjs.org/api/ngMock/service/. However, I encountered an e ...

Instantaneous data refresh using Firebase Cloud Functions

Currently, I am working on developing a chat feature in React using Firebase cloud functions. However, I am facing an issue where I do not receive updates when a user sends a message. Below is the code snippet that I have been working with: const app = a ...

Navigating the DOM with jQuery and iterating through tables without specific IDs

Coldfusion and CF markup code can be ignored as this is a jQuery question. In the table below, which can be looped over up to 200 times, users can enter a system name into the 'System Name' field and click the "CHECK" button. This triggers an aj ...

Are there any options for a JavaScript coding platform that can be used on a tablet device?

As someone who frequently travels by train, I recently purchased an Android tablet and have a strong desire to learn JavaScript. While I can read books on my tablet, I am eager to also be able to program on it. Are there any options available for develop ...

Prior to the event - Utilizing Jquery for Datepicker functionality

I have successfully blocked certain dates in the jquery datepicker. Now, I would like to utilize the "beforeShowDay function" after the datepicker is displayed to check if a specific list of dates are available or not. For each date in the list, I want t ...

When attempting to log an AJAX/JSON request, the console.log statement is displaying 'undefined'

Being new to AJAX, I have encountered an issue that I believe others may have faced as well. Despite numerous attempts and extensive research, I am unable to find a solution to this error. I am confident that it is something simple. Any help would be gre ...

Using JavaScript to Redirect to Homepage upon successful Ajax response on local server

I need assistance with redirecting to the Homepage from the SignIn Page once the user credentials have been validated. The response is working correctly, and upon receiving a successful response, I want to navigate to the Homepage. My setup involves Javasc ...

The Angular model finally refreshes its values after a console.log() is executed

UPDATE After further investigation, I discovered that the issue was not related to Angular itself, but rather a mistake in the update function within the node server controller. I have provided the fix below for reference, and decided to keep this questio ...

Update the content inside a <p> tag dynamically using Javascript based on the selected option

Struggling with Javascript and need some guidance. I have a select box with 4 options, and I want to update the contents of a <p> tag with an id of pricedesc based on the selected option. Here is my current code: function priceText(sel) { var l ...

Dealing with a problem in CSS Flexbox while aligning child elements using the `align-self`

Check out this test code here - https://jsfiddle.net/8dgeh9r3/1/ I have set up two scenarios to simulate. In the first scenario, there is a main parent flex container with multiple intermediary parents (which are also flex boxes that inherit properties fr ...

The parameter in a JavaScript for loop

I'm struggling with my Javascript skills. Here is the code snippet I am currently working with: var information = [ {"Column":"","keyw":"","Column_3":"day of march 2011 to someother numeric" ...

Is it possible to transform this nested promise into a chain instead?

This situation is just a scenario | then (items) | then (items, actions) getItems() | getActions(for:items) | apply(actions -> items) :promise | :promise | model <= items | | :sync ...

Creating an Ajax event for a weekly calendar

Utilizing the calendar feature in one of my applications can be found here: https://github.com/themouette/jquery-week-calendar Although I have successfully implemented pulling events into the calendar with an ajax call, there seems to be an issue when try ...

Arrange the table in alphabetical order and categorize it

Hello there! I am currently working on creating a well-organized table list where names are sorted into specific categories based on their starting letter. For example, names beginning with 'A' will be placed in the 'A category', while ...

creating a nested JavaScript object within another object

I need to create an object using the angular.forEach() function and then push another object while initializing all values to false. However, the current approach is causing errors. How can I achieve this correctly? Using "item.id" and "index.id" does not ...

Display a loading message using jQuery Dialog when the page is loading

I have a button that triggers a jQuery Dialog box to open. $( "#dialog" ).dialog({ autoOpen: false, title: 'Contract', height: 450, width:1100, modal:true, resizable: true }); $( ".btnSend" ).click(function() { var id=$(this).a ...

When implementing CSS object-fit: contain on an image, it may result in empty spaces surrounding the image

Within my image-wrapper container, I have an image with a variable resolution. The container itself has fixed dimensions for height and width. My goal is to center the image within the container both horizontally and vertically while adding a box-shadow e ...

Encountering issues with Vue routing while utilizing webpack. The main page is functional, however, subpaths are resulting in

Before implementing webpack, my vue routing was functioning properly. However, I encountered several loader issues and decided to use webpack. After setting up webpack, the main page loads correctly, but all of my routes now result in a 404 error. I have ...

When attempting to delete an item from Firebase using React, the re-render results in the item being

I'm currently in the process of developing an app to enhance my React skills, and I've chosen Firebase for data storage. Everything seems to be working fine as all items from Firebase are rendering properly. However, I've encountered an issu ...

The Issue of Anti Forgery Token Not Functioning Properly with Ajax JSON.stringify Post

I have been attempting to utilize an Anti Forgery token with JSON.stringify, but despite researching multiple sources, I have not been successful. Below is my AJAX code for deleting some information without any issues. Upon adding the anti forgery token, I ...