Evolution of removeclass() Functionality in HTML Table Manipulation

Can someone please advise me on how to include a transition in the removeClass method?

When I add classes, the transition-duration works perfectly fine. However, when I try to remove a class by clicking the 'undo' button, the transition effect does not seem to work.

Is there a way to ensure that the transition-duration is applied even when using the removeClass method?

Thank you in advance!

$(function() {
  let clicked = [];
  $("td").click(function() {
    let clickedID = $(this).attr('id');
    clicked.push(clickedID);
    $(this).addClass("aqua");
  });
  $("#undo").on('click',() => {
    if(clicked.length) {
      let lastClicked = clicked.pop();
      $(`td#${lastClicked}`).removeClass("aqua");
    }
  })
});
.aqua{
  background-color: aqua;
  transition-duration: 0.4s;
}


td {
  padding: 5px
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <td id="1">1</td>
  <td id="2">2</td>
  <td id="3">3</td>
  <td id="4">4</td>
  <td id="5">5</td>
  <td id="6">6</td>
  <td id="7">7</td>
  <td id="8">8</td>
  <td id="9">9</td>
  <td id="10">10</td>
</table>

<button id="undo">undo</button>

Answer №1

Removing the aqua class also results in losing the transition property.

$(function() {
  let clicked = [];
  $("td").click(function() {
    let clickedID = $(this).attr('id');
    clicked.push(clickedID);
    $(this).addClass("aqua");
  });
  $("#undo").on('click',() => {
    if(clicked.length) {
      let lastClicked = clicked.pop();
      $(`td#${lastClicked}`).removeClass("aqua");
    }
  })
});
.transition td{
  transition-duration: 0.4s;
}
.aqua{
  background-color: aqua;
}


td {
  padding: 5px
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="transition">
  <td id="1">1</td>
  <td id="2">2</td>
  <td id="3">3</td>
  <td id="4">4</td>
  <td id="5">5</td>
  <td id="6">6</td>
  <td id="7">7</td>
  <td id="8">8</td>
  <td id="9">9</td>
  <td id="10">10</td>
</table>

<button id="undo">undo</button>

Answer №2

Simply include a transition in the TD element.

$(function() {
  let clicked = [];
  $("td").click(function() {
    let clickedID = $(this).attr('id');
    clicked.push(clickedID);
    $(this).addClass("aqua");
  });
  $("#undo").on('click',() => {
    if(clicked.length) {
      let lastClicked = clicked.pop();
      $(`td#${lastClicked}`).removeClass("aqua");
    }
  })
});
.aqua{
  background-color: aqua;
}


td {
  padding: 5px;
  transition: 0.3s ease-in-out all;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <td id="1">1</td>
  <td id="2">2</td>
  <td id="3">3</td>
  <td id="4">4</td>
  <td id="5">5</td>
  <td id="6">6</td>
  <td id="7">7</td>
  <td id="8">8</td>
  <td id="9">9</td>
  <td id="10">10</td>
</table>

<button id="undo">undo</button>

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

JavaScript: Locate all the Names of the Columns in the Grid

Is there a way to utilize Javascript to locate the role = ColumnHeader and then extract the Text field (ProductNumber) below it? I am essentially looking to gather all grid column names into an array for Test Automation purposes. https://i.sstatic.net/Id ...

Error encountered: iPad3 running on iOS7 has exceeded the localStorage quota, leading to a

Experiencing a puzzling issue that even Google can't seem to solve. I keep getting the QuotaExceededError: DOM Exception 22 on my iPad3 running iOS7.0.4 with Safari 9537.53 (version 7, WebKit 537.51.1). Despite turning off private browsing and trying ...

Is it possible for jquery JSON AJAX to block all users?

On my website, I use AJAX to load an RSS feed on the client side when the page loads. If a user repeatedly presses F5, could the owner of the RSS feed ban my entire website instead of just that one user? This would prevent others from loading the RSS feed ...

Issue: jQuery Datatable not functioning properly while attempting to populate the table through JavaScript.Explanation: The

I'm currently working on populating a table using JavaScript. However, I've encountered an issue where the table is being populated but it shows "No data available in table". Furthermore, when I try to interact with the data, it disappears. This ...

How can I transfer the total and counter values to a different PHP page and store them in a text box before saving them to the database?

I need to transfer the total and counter values to another PHP page using a text box and then save them to a database. { var price =10; //price $(document).ready(function() { var $cart = $('#selected-seats'), //Sitting Are ...

POST request returned a null response in the xhr.responseText

Check out the structure of my JavaScript code: var xhr = new XMLHttpRequest(); xhr.open("POST", "http://myajaxurl.com/lyric", true); var data = "lyric"; xhr.onreadystatechange = function() { if (xhr.readyState == 4) { console.log(xhr.responseText); ...

"Exploring the intersection of Jest and GraphQL through integration testing with error

Currently, I am working on integration tests for my db and graphql server using jest as my test runner and assertions library. During each test, I verify that there are no errors by checking: const result = await graphql(schema, query); expect(result.err ...

What is the process for including a collection of objects in chartjs?

Is there a way to add an array of objects to chartjs? In the controller of the view, an array is passed to the Twig template using the variable {{cg}}. However, I am encountering an error that suggests the array I am working with is actually an array of ar ...

How does Python interpret arrays from JavaScript?

As part of my API setup, I am sending parameters to a python script. One of these parameters happens to be a JavaScript array. Interestingly, when I check the array in Python, it only shows the first index. Here is the snippet of my Angular JS get request ...

When selecting filter options, the posts/images now overlap before transitioning into a masonry view upon resizing the window

I have implemented a filter on my posts to arrange them based on popularity, style, and country. Screenshots: Screenshot 1: Everything looks good when the page loads, displaying all posts/images properly in masonry layout. Screenshot 2: Issue occurs whe ...

Unexpected Results from Div Positioning

Here is the PHP code snippet I am working on: <?php include 'header-adminpanel.php'; ?> <div class="container"> <div class="body-content"> <div class="side-left"><?php include 'adminproduct_sidebar.ph ...

What is causing the space between the images in my flex column?

I am trying to create a layout with two columns and two rows. The first row should have text in the first column and an image in the second column, while the second row should have an image in the first column and text in the second. My goal is to make th ...

Modify certain user configurations without altering all settings in one specific form using PHP and MySQL

I am in the process of developing a PHP website with data stored in a MySQL database. Currently, I have completed the sign-up, login, and logout functionalities. However, I am looking to enhance user experience by adding a section where users can adjust th ...

Prevent the repositioning of a tooltip due to overflow in Bootstrap 5.2 and Popper JS Tooltips

Can someone please provide an example of how to create a tooltip using Bootstrap 5.x and Popper JS, where the tooltip remains fixed to the associated element even if the page overflows and scroll bars appear? I've attempted to work with boundaries an ...

Issue with toggling options in q-option-group (Vue3/Quasar) when using @update:model-value

I'm a newcomer to the world of Quasar Framework and Vue.js. I recently tried implementing the q-option-group component in my simple application but encountered an issue where the model-value and @update:model-value did not toggle between options as ex ...

Importing data from json into HTML/JavaScript

For a small project, I am working on adding product data to a JSON file and then loading that data into the JavaScript section of my HTML code. However, I keep encountering errors where Microsoft Visual Code indicates that the object I am trying to load is ...

I'm in need of assistance with Laravel 6 to implement a search functionality that can search for users by name, specialty, and country

Whenever I click the Search button, an error (Undefined nom) pops up. I am looking for someone who can help me troubleshoot and fix my code. Here is the code that needs correction: Controller: doctors.php <?php namespace App\Http\Cont ...

Testing an Angular service using Karma, Jasmine, and JSPM

I am currently working on writing a unit test for my Angular 1 application using JSPM with Karma and Jasmine. Below is a snippet of my Karma configuration (jspm section) : jspm: { meta: { 'jspm_packages/github/angular/angular.js': { ...

Adjust the size of a DIV once all AJAX requests have been completed within a .getScript function in JavaScript

I'm facing an issue with dynamically resizing a DIV after all content is loaded using jQuery. This is the structure of my HTML page: html <div id="limit" style="position: relative; overflow: hidden;"> <div class="contentpanel> ...

The submission process continues to load indefinitely without displaying any data

I have set up a donation form where the user can submit their donation. The issue I am facing is that, although the data is being updated in the backend and stored in the database correctly, the AJAX request is not receiving the return data. I have a loade ...