4

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>
1
  • Have you tried to solve your task yourself? If yes - how? And what have you failed at? Commented Mar 26, 2013 at 9:30

3 Answers 3

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

DEMO

Sign up to request clarification or add additional context in comments.

Comments

1

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();
});

Comments

0

you cam use filter:

var tableRow = $("td").filter(function() {
    return $(this).text() == '2013-03-21';
}).closest("tr");

or use contain:

var tableRow = $("tr:has(td:contains('2013-03-21'))");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.