Solved

Jquery Compare Two Tables Cell by Cell?

Posted on 2014-04-03
4
1,948 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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)

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now