Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extend html table highlight row and column and make current cell a different color

I found and example of highlighting a row and column on hovering with mouse.

https://stackoverflow.com/a/28312853/139698

How can I make the one cell that the mouse is hovering over to a different color, but keeping the the highlighted row and column as well?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<html>
<head>
    <title></title>
    <style>
        .hovered {
            background-color: teal;
        }
    </style>
    <script src="Scripts\jquery-3.6.0.min.js" type="text/javascript"></script>
</head>
<body>
    <table border="1">
        <tr>
            <td>row1 col1</td>
            <td>row1 col2</td>
            <td>row1 col3</td>
        </tr>
        <tr>
            <td>row2 col1</td>
            <td>row2 col2</td>
            <td>row2 col3</td>
        </tr>
        <tr>
            <td>row3 col1</td>
            <td>row3 col2</td>
            <td>row3 col3</td>
        </tr>
    </table>
    <script>
        $(document).ready(function () {
            $('td').hover(function () {
                $(this).parent('tr').toggleClass('hovered');
                $('td:eq(' + this.cellIndex + ')', 'tr').toggleClass('hovered');
            });
        });
    </script>
</body>
</html>

>Solution :

Continuing on the linked SO answer


You can add a class to the component (this) itself, and apply some css for it:

$(this).toggleClass('hovered-cell')
.hovered-cell {
  background: orange;
}

$('td').hover(function() {
    $(this).toggleClass('hovered-cell')
  $(this).parent('tr').toggleClass('hovered');
  $('td:eq('+this.cellIndex+')','tr').toggleClass('hovered');
});
table {
  border-spacing: 0px;
}

td {
  border: 1px solid #bbb;
  padding: 0.2em;
}

tr:first-child, td:first-child {
  background: lightgrey;
}

.hovered {
  background: yellow;
}

.hovered-cell {
  background: orange;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr><td>    <td>Column1<td>Column2<td>Column3<td>Column4<td>Column5
  <tr><td>Row1<td>Data1  <td>Data2  <td>Data3  <td>Data4  <td>Data5
  <tr><td>Row2<td>Data1  <td>Data2  <td>Data3  <td>Data4  <td>Data5
  <tr><td>Row3<td>Data1  <td>Data2  <td>Data3  <td>Data4  <td>Data5
</table>

Based on OP’s comment, here’s the same idea, with a onClick that will keep the rows/cols colored until the next click. Just to give an idea, can be improved using eg functions to reduce duplicate code

const removePersistant = () => $('.hovered-per').removeClass("hovered-per");

$('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).toggleClass('hovered-per')
    $(this).parent('tr').toggleClass('hovered-per');
    $('td:eq('+this.cellIndex+')','tr').toggleClass('hovered-per');
});
table {
  border-spacing: 0px;
}

td {
  border: 1px solid #bbb;
  padding: 0.2em;
}

tr:first-child, td:first-child {
  background: lightgrey;
}

.hovered {
  background: yellow;
}

.hovered-cell {
  background: orange;
}

.hovered-per {
  background: purple;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr><td>    <td>Column1<td>Column2<td>Column3<td>Column4<td>Column5
  <tr><td>Row1<td>Data1  <td>Data2  <td>Data3  <td>Data4  <td>Data5
  <tr><td>Row2<td>Data1  <td>Data2  <td>Data3  <td>Data4  <td>Data5
  <tr><td>Row3<td>Data1  <td>Data2  <td>Data3  <td>Data4  <td>Data5
</table>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading