JavaScript How to highlight a table row upon hover conditionally based on table cell values with CSS and jQuery?

So I have an HTML table populated with negative and positive values. I want to query the specific values and then upon hovering over the table values less than 0 I want to highlight the row with a red background color and for values greater than or equal to 0 should highlight with a blue background.

My current attempt is:

$(document).ready(function(){
    var rows = $("#htmlTable").find('tbody').find('tr');
    $.each(rows, function(key, value){
        var rowData = $(rows[key]).find('td:eq(1)').html();
        if (rowData < 0) {
            $("tr").hover(function(){
                $(this).addClass('.table-style-1 tbody > tr:hover');
            }
        }
        else {
            $("tr").hover(function(){
                $(this).addClass('.table-style-2 tbody > tr:hover');
            });
        }
    });
});

The CSS classes are as follows:

.table-style-1 tbody > tr:hover {
    background-color: red;
}

.table-style-2 tbody > tr:hover {
    background-color: blue;
}

My above code is correctly getting the data from the table as can been seen with a simple console.log(rowData) line so I am assuming that my problem is with my implementation of addClass() and hover() functions. The console isn't showing any syntax errors but the table has no highlight functionality upon hovering. Any help would be greatly appreciated.

Answer:1

Since you're getting a string with rowData = $(rows[key]).find('td:eq(1)').html();you shoud cast the value into an integer when you do a comparison, e.g.

if (parseInt(rowData, 10) < 0) { ...

Also why exactly are you adding a class like .table-style-2 tbody > tr:hover? You should just add the a class to the rows e.g. negative or positive (note: you need to use $(this) and not $('tr'))

if (parseInt(rowData, 10) < 0) {
    $(this).addClass('negative');
}
else {
    $(this).addClass('positive');
}

and define your css like so

tr.negative:hover {
    background-color: red;
}

tr.positive:hover {
    background-color: blue;
}
Answer:2

url for LinkedIn advanced search: https://www.linkedin.com/vsearch/p?adv=true&trk=advsrch I am trying to fill fields and hit the submit button on the LinkedIn advanced search page using Selenium (...

url for LinkedIn advanced search: https://www.linkedin.com/vsearch/p?adv=true&trk=advsrch I am trying to fill fields and hit the submit button on the LinkedIn advanced search page using Selenium (...

  1. scrapy use selenium
  2. scrapy with selenium
  3. scrapy with selenium tutorial

i am making an app where i need 3d joystick image so that user can swipe on it and image will bend in swipped direction. For swipe down or for swipe up events what css or animation effect i should ...

i am making an app where i need 3d joystick image so that user can swipe on it and image will bend in swipped direction. For swipe down or for swipe up events what css or animation effect i should ...

Here is the select box/drop-down menu: var type_select = '<select id="type_select" style="margin-bottom:0px;">'; var i; var customer_group = <?php echo json_encode($...

Here is the select box/drop-down menu: var type_select = '<select id="type_select" style="margin-bottom:0px;">'; var i; var customer_group = <?php echo json_encode($...

Is it possible to style the hyperlink tag in CSS in such a way that when a link is clicked it opens it in a new tab or window? I know the solution in HTML and JavaScript/jQuery, yet is it possible to ...

Is it possible to style the hyperlink tag in CSS in such a way that when a link is clicked it opens it in a new tab or window? I know the solution in HTML and JavaScript/jQuery, yet is it possible to ...

  1. open links tabs
  2. links opentable click
  3. open links in tabs instead of new windows
  4. open links new tab chrome
  5. open links in tabs firefox
  6. open links new tab html
  7. open links in tabs chrome
  8. open links new tab firefox
  9. open links in tabs chrome extension
  10. open links same tab
  11. html open link tab
  12. open links in tab google
  13. open links in new tab
  14. open links in new tab chrome default
  15. open links in same tab chrome
  16. open links in new tab wordpress
  17. open links in new tab css
  18. open links in new tab chrome extension
  19. open links in new tab safari
  20. open links in new tab internet explorer