jQuery: Run function if each tr has class

AliIshaq

I have a table where trs are being assigned class with js.

I have another div that I want to reflect it on. However, my each() function doesn't seem to be working properly.

I have the following JS:

$("table.result tr").each(function() {
    if($(this).hasClass("red")) {
        $("#color").addClass("red");
        //bunch of other things
    } else {
        $("#color").addClass("green");
        //bunch of other things
    }
});

And this is my markup:

<table class="result">
   <tr class="red">
       <th>Name</th>
       <th>Result</th>
   </tr>
   <tr class="red">
       <td>John Doe</td>
       <td>Pass</td>
   </tr>
   <tr class="red">
       <td>Johnathan Doe</td>
       <td>Pass</td>
   </tr>
   <tr class="red">
       <td>Jane Doe</td>
       <td>Fail</td>
   </tr>
</table>
<div id="color"></div>

What I'm trying to do is, if all the trs have a class of red, only then do I want the div to have the class of red too else it should be green. But the jQuery is only testing it on the last tr whether it has the class or not.

But it doesn't seem to be working. It's probably a very small mistake but I cannot figure it out. Here's a jsfiddle: https://jsfiddle.net/okvv330L/6/

As you can see, since one tr does not have the class red, the div should be green. But it's only checking the last tr and basing it on that.

Thanks!

AliIshaq

I got this working by using the following code:

var $rowCount = $('.result tr').length;
var $rowFill = 0;
$(".result tr").each(function() {
    if($(this).is('.red')) {
        $rowFill = $rowFill + 1;
    }
});
if($rowFill == $rowCount) {
    $('#color').addClass('red');
} else {
    $('#color').addClass('green');
}

Since the each() was only testing the last row; I got it to match the count with the number of rows. So now, it seems to work fine.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Run a jQuery function for each element with a certain class

From Dev

Jquery each function for td in tr

From Dev

jQuery run function after $.each ajax request has completed

From Dev

How to find a tr which has a specific class attached and get the details of each td - jquery

From Dev

jQuery run function only in a div that has anoher div with specific class

From Dev

jQuery run function after everything has run

From Dev

jQuery run function when get has finished

From Dev

Run jQuery function for each element separately

From Dev

Run jQuery function for each element separately

From Dev

How could I use jQuery and batch add a class (class="none") to "tr" when this "tr" has attribute valign="baseline"

From Dev

jQuery each in each function

From Dev

jQuery datatables add class to tr

From Dev

Jquery : To find the closest tr class

From Dev

jQuery each loop with has()

From Dev

How to have jQuery display the next nearest <tr> in a table and attach a jquery function to a class for multiple dropdowns

From Dev

Run a Function For Each Milliseconds

From Dev

jQuery click function on gridview tr

From Dev

Adding a class to <tr> if <a> element has text

From Dev

Adding a class to <tr> if <a> element has text

From Dev

How to add a class to each TD in a TR when the TR gets selected

From Dev

Toggle class on each tr row when clicked

From Dev

Jquery filter out the double class name with each function

From Dev

jQuery ".each()" function only applying script to last class on page

From Dev

Run a function after each function

From Dev

How to separate each tr of table by jquery

From Dev

PHP Class extending an Abstract cannot run a function that has the same name as the Abstract Class

From Dev

jquery if has class issue

From Dev

jquery if has class issue

From Dev

jQuery - if parent has not class

Related Related

  1. 1

    Run a jQuery function for each element with a certain class

  2. 2

    Jquery each function for td in tr

  3. 3

    jQuery run function after $.each ajax request has completed

  4. 4

    How to find a tr which has a specific class attached and get the details of each td - jquery

  5. 5

    jQuery run function only in a div that has anoher div with specific class

  6. 6

    jQuery run function after everything has run

  7. 7

    jQuery run function when get has finished

  8. 8

    Run jQuery function for each element separately

  9. 9

    Run jQuery function for each element separately

  10. 10

    How could I use jQuery and batch add a class (class="none") to "tr" when this "tr" has attribute valign="baseline"

  11. 11

    jQuery each in each function

  12. 12

    jQuery datatables add class to tr

  13. 13

    Jquery : To find the closest tr class

  14. 14

    jQuery each loop with has()

  15. 15

    How to have jQuery display the next nearest <tr> in a table and attach a jquery function to a class for multiple dropdowns

  16. 16

    Run a Function For Each Milliseconds

  17. 17

    jQuery click function on gridview tr

  18. 18

    Adding a class to <tr> if <a> element has text

  19. 19

    Adding a class to <tr> if <a> element has text

  20. 20

    How to add a class to each TD in a TR when the TR gets selected

  21. 21

    Toggle class on each tr row when clicked

  22. 22

    Jquery filter out the double class name with each function

  23. 23

    jQuery ".each()" function only applying script to last class on page

  24. 24

    Run a function after each function

  25. 25

    How to separate each tr of table by jquery

  26. 26

    PHP Class extending an Abstract cannot run a function that has the same name as the Abstract Class

  27. 27

    jquery if has class issue

  28. 28

    jquery if has class issue

  29. 29

    jQuery - if parent has not class

HotTag

Archive