Solved

Jquery Compare Two Tables Cell by Cell?

Posted on 2014-04-03
4
2,182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
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…
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)

726 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