Link to home
Start Free TrialLog in
Avatar of WorknHardr
WorknHardr

asked on

Jquery Compare Two Tables Cell by Cell?

I found this example that only works using === when comparing two cell values.
http://stackoverflow.com/questions/12561937/compare-two-tables-rows-and-remove-if-match

Q. How can I compare two tables cell by cell using != operator?

<table id="T1">
    <tr><td>111</td></tr>
    <tr><td>222</td></tr>
    <tr><td>333</td></tr>
</table>

<table id="T2">
    <tr><td>444</td></tr>
    <tr><td>111</td></tr>
    <tr><td>333</td></tr>
</table>

$('#T1 tr').each(function()
 {
    var leftTableCell=$(this);

    $('#T2 tr').each(function()
    {
        if($(this).html( )=== leftTableCell.html())
        {
             //Works!
            $(this).css('background-color', 'green').css('color', 'white'); 
        }
        else
        {
              //Makes ALL cells yellow in T2
            $(this).css('background-color', 'yellow')
        }
    });
});

Open in new window

Avatar of Mrunal
Mrunal
Flag of India image

Hi
can you please explain what exactly do you mean by ...

"Q. How can I compare two tables cell by cell using != operator?"
Avatar of WorknHardr
WorknHardr

ASKER

This 'if' statement works well:
                 if($(this).html( ) === leftTableCell.html())

This 'if' statement does NOT work (makes ALL cells yellow):
                if($(this).html( ) != leftTableCell.html())

I need to compare for Un-equal cell data too...
ASKER CERTIFIED SOLUTION
Avatar of Mrunal
Mrunal
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thx