ljencina77
ljencina77

Reputation: 3

compare values of cells in different rows in table using jquery

I have a dynamically generated table with php that has same rows. Need to get value from cell 1 in row 1 and value from cell 1 in row 2 and compare them. If they are the same remove entire row or hide... Do that for the whole table... Any help is appreciated.. Thanks!!

Haave this so far:

var numRows = $('table#changeSubjectKatedra tr').lenght;
var i = 0;
do {
    var cur = $('input#'+i).val();
    var next = $('input#'+(i+1)).val();
    if(cur == next){
        $('tr#'+i).remove();
        }
    i++;
} while(i<numRows);

The row in table looks like this:

<tr id='idNum'><td>someText<input type='hidden' value='someData' id='idNum'>
</td><td>someText</td></tr>  

Upvotes: 0

Views: 3506

Answers (2)

Lidor
Lidor

Reputation: 394

You can use jQuery's .each function. This should work according to the description you provided:

$('table#changeSubjectKatedra tr').each(function() {
    if (repeat == $(this).children("input")[0].value) {
        $(this).remove();
    }
    repeat = $(this).children("input")[0].value;
});

Upvotes: 2

Pavel Strakhov
Pavel Strakhov

Reputation: 40512

Note 1. You should do in on server side with PHP, not with JavaScript.

Note 2. You must use unique id for each element.

Note 3. Avoid using numerical ids of elements.

Note 4. You don't need ids at all for doing what you want.

If you still want to do it in JavaScript, I suggest you to do it this way: (live demo here)

var rows = $("#changeSubjectKatedra tr");
for(var i = 0; i <= rows.length - 2; i++) {
  if ($(rows[i]).find("input").val() ==
      $(rows[i+1]).find("input").val()) {
    $(rows[i]).remove();   
  }
}

Upvotes: 3

Related Questions