Struggling to make divs disappear using the .each() method following an ajax call?

Currently, my divs are loaded one by one on page load and everything is working perfectly. However, I encounter an issue when making an AJAX request and then using append(data) to the same container. In this scenario, my code ends up looping through all of the divs on the page instead of just the newly added ones.

So, my main concern is: how can I achieve the same .each() effect only on the new data received from a successful AJAX call?

Here's the jQuery snippet:

success: function(data){
    $('#profileLinks').append(data);  
   
    $('.eachLink').each(function(i) {
        $(this).fadeOut(0).delay(100*i).fadeIn(500);
    }); 

} 

And here's the corresponding HTML:

<div id="profileLinks" class="floatLeft">
    <div class="eachLink floatLeft">Div 1</div>
    <div class="eachLink floatLeft">Div 2</div>
</div>

Answer №1

let $info = $(info);
$info.find(".eachItem").addClass("pending-animation").appendTo("#userItems");
$(".eachItem.pending-animation").each(/* custom animation logic */);

Answer №2

One potential solution is to eliminate the "class" attribute altogether:

success: function(data){
    $('#profileLinks').append(data);  

    $('.eachLink').each(function(i) {
        $(this).removeClass(".eachLink");
        $(this).fadeOut(0).delay(100*i).fadeIn(500);
    }); 

} 

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

Analyzing cookie data and utilizing jQuery for data presentation

Let's brainstorm. I'm currently working on a chat bar and have successfully implemented its functionality. However, I am facing a challenge in maintaining the continuity of the chat boxes while navigating through different pages on the website. ...

Database interaction menu for retrieval and posting of options

Is there a way to create an option list that retrieves data dynamically from a database and allows the user to select a record to post ($_POST) to the database? I have attempted to do this but have encountered an issue where the record is not being posted: ...

What is the best way to combine several plugins in tinymce?

I experimented with a basic code snippet to test tinyMCE, and everything is functioning properly. However, I encountered an issue when attempting to add multiple plugins simultaneously. In this instance, I utilized the tinymce CDN for reference. Below is ...

TableTools for DataTables

DataTables is functioning properly on my website, but I am facing issues with getting the tableTools css to work correctly. Specifically, I'm unable to get the copy_csv_pdf_print.swf file to load. While I can render the table with some styling, other ...

The functionality of ngClass fails to toggle the value appropriately when utilizing an object within a component

When implementing the code below to toggle the class, everything runs smoothly <p [ngClass]="idPresent ? 'alert alert-success' : 'alert alert-danger'"> Working </p> After changing the value of IdPresent in the c ...

I can retrieve my information from the controller by utilizing Ajax

I am currently developing a project in asp.net mvc4. Specifically, I am working on a form that includes a Select element containing a list of contacts: <select id="choix_contact"> <option>Choose a contact</option> @if (ViewBa ...

Steps to export Excel in Laravel without using a model:

I've already reviewed Laravel's export to Excel functionality, but my project is unique in that it doesn't involve a model. How can I export to Excel in Laravel without needing a model? I'm working with Laravel 8 and Maatwebsite 3. &l ...

Treating ajax page, jquery does not alter the input value

Issue resolved! You can find the working code at the bottom. First and foremost, please accept my apologies for any language errors. If possible, could you kindly rephrase my query? I am encountering difficulty changing the input value using jQuery from ...

Is it possible to insert JavaScript code with the <link> element?

Is it possible to include JavaScript code using the <link> tag on my website? For instance, if I have a JavaScript file named test.js that includes the simple code alert('hello'); Can I trigger a popup window to appear by using the follow ...

What is the method for obtaining the translate property from a group?

I have multiple SVG groups, each containing a variety of child elements. When a click event occurs on a group, I want to move all groups and their children. I need to access the translate properties of the clicked group so I can adjust the others accordin ...

A tutorial on converting a tree structure into a JSON object using jQuery

Struggling to create a JSON object from the tree class, specifically needing only the span values. The structure of the tree is dynamic and nested, so utilizing a recursive function seems like the most suitable approach. Currently able to extract the keys ...

Adding an <a> tag within an <li> element can lead to complications

I'm having trouble inserting an anchor tag into a list item. I can't figure out why it's not working. navListItem = '<li class="p-list-item"></li>'; mainLink = '<a class="main-a" href="' + ...

Is it possible to disable hovering effects on inner elements?

Disable the hover pseudo-class on child elements. Prevent the hover effect on child elements when hovering over them. Is there a way to achieve this? .link:hover { background: #499a75; } <a href="#" class="link"> Test <ul> <li&g ...

Retrieve the current font style with Kendo UI Editor

Recently, I've been facing some challenges while working with the Kendo UI Editor. Specifically, I'm struggling to retrieve the current font style. My goal is to use JavaScript or jQuery to obtain the following values: Values I am looking for: ...

Personalize the image upload field in a straightforward form and use carrierwave

I've been facing challenges with customizing the simple form field for image upload (Carrierwave) in my Rails application. Current design: Desired outcome: My approach involved inspecting the HTML generated by simple form helpers: <%= f.input : ...

Angular: Customizing table cell color based on condition

I am having trouble with changing the cell color of an object in my html table if a certain variable changeColor is true. I am utilizing angular for this task. <tr ng-repeat="list in results"> <% if (!{{list.changeColor}} ) %> <% { ...

Ways to substitute numerous instances of a string in javascript

I have experience in developing websites using reactjs. I usually implement restAPI's with java and work with liferay CMS. In one of my projects, I created a shortcode for accordion functionality like this: ('[accordion][acc-header]Heading 1[/ac ...

Pull HTML code from element using XPath in Python Selenium

I want to extract HTML codes from a specific element using XPath. Here is the XPath: //*[@id=":nn"]/div[1] Additionally, I need to download an image from the same element. What is the best way to retrieve these HTML codes? ...

Glitch in Safari 6: Round Corners Issue

Greetings! Upon observing the image provided, an unusual behavior can be noticed in Safari 6 concerning round corners. { border-radius: 5px 5px 5px 5px; } Upon hovering over the elements, a thin line appears that seems to accumulate with each subsequent ...

How can I use jQuery to access the parent node in an XML document?

I have been trying to extract the top-level 'label' attribute from the XML code below using jQuery, but I haven't had any luck so far. I have already converted it into a DOM object, but the results are not what I expected. Does anyone have a ...