When I click each cells, layer counts of following 20 cells will increase.
But my desired result is to decrease layer counts when I red class is removed.
At this time, it always increases and I couldn't come up with how to decrease layer counts in the following 20 cells.
It doesn't matter how to write if we can achieve it.
Is there any sophisticated way to achieve this?
const layerArray=["layer1","layer2","layer3","layer4"];
var ArrLen=layerArray.length;
$(function() {
$("td").click(function() {
$(this).children().toggleClass('red');
index=$("td").index(this);
$("td").slice(index+1,index+20).each(function() {
var classIndex = $(this).data('layer');
classIndex++;
if(classIndex < (ArrLen-1)) {
$(this).addClass(layerArray[classIndex-1]);
} else {
$(this).addClass(layerArray[ArrLen-1]);
}
$(this).data('layer',classIndex);
});
});
});
td {
transition-duration: 0.5s;
border: solid black 1px;
cursor:pointer;
}
div{
padding: 5px;
}
table {
border-collapse: collapse;
}
.red{
background-color:red;}
.layer1{
background-color: hsl(60, 90%, 90%);}
.layer2{
background-color: hsl(40, 90%, 90%);}
.layer3{
background-color: hsl(20, 90%, 90%);}
.layer4{
background-color: hsl(0, 90%, 90%);}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id=calendar></div>
<script>
Html =' '
Html += '<table>';
let i =0;
for (let w = 0; w < 10; w++) {
Html += '<tr>';
for(let d=0; d<10;d++){
i=i+1;
Html += '<td data-layer=0>'+'<div>'+i+'</div>'+'</td>'
}
Html += '</tr>';
}
Html +='</table>'
document.querySelector('#calendar').innerHTML = Html;
</script>