Solved

Jquery Compare Two Tables Cell by Cell?

Posted on 2014-04-03
4
2,084 Views
Last Modified: 2014-04-05
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

0
Comment
Question by:WorknHardr
  • 2
  • 2
4 Comments
 
LVL 22

Expert Comment

by:Mrunal
ID: 39975690
Hi
can you please explain what exactly do you mean by ...

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

Author Comment

by:WorknHardr
ID: 39975730
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...
0
 
LVL 22

Accepted Solution

by:
Mrunal earned 500 total points
ID: 39977681
Hi
Please try with !== instead of !=

check this:
http://jsfiddle.net/R54w4/
0
 

Author Closing Comment

by:WorknHardr
ID: 39979989
thx
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question