0

Possible Duplicate:
JQuery: How to select rows from a table

I want to select specific rows of a table based on certain conditions using jquery. For example, If a table has 25 rows with a checkbox column to check each row, I want to specify a condition and if that condition is true, then only the checkbox should be checked, for example, 5 rows satisfy that condition, so only 5 rows should be checked. This selection may be dependent on other columns of the table. How do I achieve this with jquery?

Community
  • 1
  • 1
Manoj Agarwal
  • 365
  • 2
  • 17

2 Answers2

1

You need something like this,

var chkbox = $('#yourTableId tr').eq(indexOfRowYouWant).find(':checkbox').attr('checked', true);
Adil
  • 146,340
  • 25
  • 209
  • 204
1
$( 'tr', table ).each(function () {

    this // refers to the current TR element

    // check if the condition is met for this row
    var conditionMet = ...

    // and set the checked state accordingly 
    $( '.checkbox', this )[0].checked = conditionMet;

});

where table is a reference to your TABLE element, and "checkbox" is the class of the <input type=checkbox> element within each row.

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385