How to undo class change in html table

Heisenberg :

When I try to undo class change,I suffer some problems.

I could get clicked element, but my desired result is to get class changed element and undo it.

For example in my snippet,I would like to undo all the class change at the same time.

In other words, I would like to getclass changed element array.

Are there any method?

Thanks

let clicked=[];

$(function() {
  $("td").click(function() {
    $(this).nextAll(':lt(4)').addClass('color');
     let clickedID=$(this).attr('id');
     clicked.push(clickedID);
});
});

$(function() {
  $("#btnCancel").on('click',() => {
    if(clicked.length) {
      let lastClicked = clicked.pop();
      $(`td#${lastClicked}`).removeClass();
    }
  });
  });
table td {
  width: 20px;
  overflow: hidden;
  display: inline-block;
  white-space: nowrap;
  border: 1px solid gray;
  text-align: center;
  padding: 5px;
  cursor: pointer;
}
.color {
  background-color: hsl(60,100%,60%);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
<tr>
<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>
</tr>
</table>

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

Rayon :

.removeClass([className]) Remove a single class, multiple classes, or all classes from each element in the set of matched elements.

$('td#${lastClicked}').nextAll(':lt(4)').removeClass('color'); would return the last selected/updated elements.

let clicked = [];

$(function() {
  $("td").click(function() {
    $(this).nextAll(':lt(4)').addClass('color');
    let clickedID = $(this).attr('id');
    clicked.push(clickedID);
  });
});

$(function() {
  $("#btnCancel").on('click', () => {
    if (clicked.length) {
      let lastClicked = clicked.pop();
      $(`td#${lastClicked}`).nextAll(':lt(4)').removeClass('color');
    }
  });
});
table td {
  width: 20px;
  overflow: hidden;
  display: inline-block;
  white-space: nowrap;
  border: 1px solid gray;
  text-align: center;
  padding: 5px;
  cursor: pointer;
}

.color {
  background-color: hsl(60, 100%, 60%);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
  <tr>
    <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>
  </tr>
</table>

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to undo class change and store class data in html table

How to undo a change in QAbstractTableModel?

How to undo the change in Firestore?

how to undo CHANGE MASTER TO

How to undo changes of framework change?

How to change a html class with Javascript?

How to change class of elements in adjacent rows of HTML table using jQuery's nextAll() selector

how do you change a active class in html

How to change alignment of each cell in html table?

how to change the value of table cells with a slider in html

How to change Stargazer HTML table width?

How to change dataframe to html table in django ?

How to change the height of table rows in HTML?

How to add class by referring to array in html table

change html value of class

Change HTML in table TD

How to change class of table row below clicked one?

How to change Labels and Table View from another Class?

JQuery: How to change active table class from another link?

How to change class of dymanic row of a table in Angular2

jQuery How to change class for each table data row?

How to change the timestamp format shown when undo/redo in vim?

How to change the background color of the DataGridView row and undo on hover?

Git How to undo recent merge commit then change to rebase

How to undo ALTER TABLE ... ADD PARTITION without deleting data

How can I undo the reordering of table view cells on the tap of a button

Qtreewidget -- undo the selection change

How to count class change within rows in html tables

How to change class to currently hovered cells in html tables