Compare two row tables and delete if they match

Can someone help me in jQuery? I have two tables on my site leftTable and rightTable with the same column names. leftTable I populate from the database, but rightTable it just contains a few rows. What I would like to do is not to show (or delete) in leftTable those lines that exist in rightTable !

I tried this:

$("#tableLeft tr").each(function(){
    if($(this).find("td")[0].innerHTML == $("#tableRight tr").find("td")[0].innerHTML)
    {
        $(this).remove;
    }
});
+5
source share
2 answers

I assume you have something like this:

<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>

To remove rows from a table with id = "T2", you can do something like this:

$('#T1 tr').each(function(){

    var currentRowHTML=$(this).html();

    $('#T2 tr').each(function(){
        if($(this).html()===currentRowHTML){
            $(this).remove();
        }
    });
});
+2
source

$(function(){
    $('#btn').on('click', function(e){
        $('#right_table tbody tr').each(function(){
            var row=$(this).html();
            $('#left_table tbody tr').each(function(){
                if(row==$(this).html()) $(this).remove();
            });
        });
    });
});​

DEMO.

, (HTML), , (/id) , .

+2

All Articles