Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select table row and check checkbox using JQuery

Tags:

jquery

I have a table and I want to select the rows that satisfy my criteria and check their respective checkbox. Let's say I want to get the rows with date 2013-03-21. How can I do this using JQuery?

<table>
<tr>
    <td>
        Record1
    </td>
    <td>
        2013-03-21
    </td>
    <td>
        <input type="checkbox"/>
    </td>
</tr>
<tr>
    <td>
        Record2
    </td>
    <td>
        2013-03-22 
    </td>
    <td>
        <input type="checkbox"/>
    </td>
</tr>
<tr>
    <td>
        Record3
    </td>
    <td>
        2013-03-21
    </td>
    <td>
        <input type="checkbox"/>
    </td>
</tr>
</table>
like image 625
kimbebot Avatar asked Dec 21 '22 08:12

kimbebot


2 Answers

$("table tr").each(function () {
    if ($(this).find("td:eq(1)").text().trim() == '2013-03-21') {
     $(this).find("input[type=checkbox]").attr("checked", true);
  }
});

DEMO

like image 200
Anujith Avatar answered Jan 11 '23 17:01

Anujith


You can use filter(), You better assign some id to table and use that in selector and be sepecific

Live Demo

trs = $('td').filter(function(){
 if($.trim($(this).text()) == '2013-03-21')
    return $(this).parent();
});
like image 43
Adil Avatar answered Jan 11 '23 16:01

Adil