Someone kindly helped me with having row and column highlighting and even permenant ones when I click on a cell. However, I'm expecting row 3 col 4 to be green just like row 1 col 2 and I'm not sure why that is.
In other words, the hover highlighting should appear underneath the permanent highlighting
Additional Question: How would you do all this highlighting in ReactJS, would you still use JQuery?
$(function() {
const removePersistant = () => {
$('.hovered-per').removeClass("hovered-per");
$('.hovered-cell').removeClass("hovered-cell");
};
$('td').hover(function() {
$(this).toggleClass('hovered-cell')
$(this).parent('tr').toggleClass('hovered');
$('td:eq(' + this.cellIndex + ')', 'tr').toggleClass('hovered');
$('.hovered-per').removeClass("highlight");
});
$('td').click(function() {
removePersistant();
//$(this).addClass('hovered-cell')
$(this).parent('tr').toggleClass('hovered-per');
$('td:eq(' + this.cellIndex + ')', 'tr').toggleClass('hovered-per');
});
$("button").click(removePersistant)
});
table {
border-spacing: 0px;
}
td {
border: 1px solid #bbb;
padding: 0.2em;
}
/*
tr:first-child, td:first-child {
background: lightgrey;
}
*/
.hovered-per {
background-color: green;
color: white;
}
.hovered {
background: yellow;
}
.hovered-cell {
background: orange;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" type="text/javascript"></script>
<table border="1">
<tr>
<td>row1 col1</td>
<td>row1 col2</td>
<td>row1 col3</td>
<td>row1 col4</td>
</tr>
<tr>
<td>row2 col1</td>
<td>row2 col2</td>
<td>row2 col3</td>
<td>row2 col4</td>
</tr>
<tr>
<td>row3 col1</td>
<td>row3 col2</td>
<td>row3 col3</td>
<td>row3 col4</td>
</tr>
<tr>
<td>row4 col1</td>
<td>row4 col2</td>
<td>row4 col3</td>
<td>row4 col4</td>
</tr>
</table>
<button id='clear'>clear</button>
