2009/08/28 09:03

To compare two HTML tables on web page in real time


When you need to compare two tables on web page, you can simply read the content with innerHTML tag easily. But you need to analyze the table content. Following is the simple example to analyze two table content.

<script lanugage="text/javascript">
function CompareTables(table1,table2)
{
  var instHasChange = false;
  for(var i=0; i < table1.rows.length; i++)
  {
    var changes =RowExists(table2,table1.rows[i].cells[0].innerHTML,table1.rows[i].cells[1].innerHTML);
    if(!changes[0])
    {
     table1.rows[i].style.backgroundColor = "red";
     instHasChange = true;
    }
    else if(changes[1])
    {
    table1.rows[i].style.backgroundColor = "orange";
    instHasChange = true;
    }
  }
  for(var i=0; i < table2.rows.length; i++)
  {
    var changes = RowExists(table1,table2.rows[i].cells[0].innerHTML,table2.rows[i].cells[1].innerHTML);
    if(!changes[0])
    {
     table2.rows[i].style.backgroundColor = "#00CC33";
     instHasChange = true;
    }
    else if(changes[1])
    {
    table2.rows[i].style.backgroundColor = "orange";
    instHasChange = true;
    }
  }
  return instHasChange;
}

function RowExists(table,columnName,columnValue)
{
  var hasColumnOrChange = new Array(2);
  hasColumnOrChange[0] = false;
  hasColumnOrChange[1] = false;
  for(var i=0; i < table.rows.length; i++)
  {
    if(table.rows[i].cells[0].innerHTML == columnName)
    {
    hasColumnOrChange[0] = true;
    if(table.rows[i].cells[1].innerHTML != columnValue)
    hasColumnOrChange[1] = true;
    }
  }
  return hasColumnOrChange;
}
</script>

Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject Buy phentermine online.

    Tracked from Order phentermine online. 2010/03/20 16:59 delete

    Buy phentermine online without prescription. Phentermine online. Phentermine online pharmacy.

  2. Subject Phentermine blue.

    Tracked from Buy cheap phentermine. 2010/04/15 18:10 delete

    Phentermine.

  3. Subject Order phentermine online.

    Tracked from Online phentermine. 2010/04/21 12:11 delete

    Phentermine online purchase. Cheapest phentermine online free shipping.