Solved

Jquery Compare Two Tables Cell by Cell?

Posted on 2014-04-03
4
2,120 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

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.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

756 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