marky
marky

Reputation: 5068

jQuery - How to find an element with a specific value in a table row

I have a table with the following structure:

<table id="tblBranchCoverage">
    <thead>...</thead>
    <tbody>
        <tr class="coverageRow">
            <td class="countyCovered">
                <label="branchCountyCovered coverageDisplay">Barrow</label>
                                ...
            </td>
            <td>
                ...
            </td>
        </tr>
        <tr class="coverageRow">
            <td class="countyCovered">
                <label="branchCountyCovered coverageDisplay">Cook</label>
                ...
            </td>
            <td>
                ...
            </td>
        </tr>
    </tbody>
</table>

I'm trying to find the row that has a label with a specific text.

I can't figure anything out other than this (which doesn't work):

$("#tblBranchCoverage tbody tr").find('label[text="Barrow"]')

I verified it's not working by testing the selector in the console.

What's the correct way to do this?

Upvotes: 0

Views: 1315

Answers (2)

Jeff
Jeff

Reputation: 2443

try contains:

http://docs.jquery.com/Selectors/contains#text

then you should be able to use parents to filter up:

http://docs.jquery.com/API/1.2/Traversing/parents

so it should be something like

$("label:contains('Barrow')").parents("tr.coverageRow");

Upvotes: 1

Bassam Mehanni
Bassam Mehanni

Reputation: 14944

This should do the trick, it will filter down your selection based on the label text

$("#tblBranchCoverage tbody tr label").filter(function(){
  return (/Barrow/i).test($(this).text())
})

or

$("#tblBranchCoverage tbody tr label").filter(function(){
  return $(this).text() == "Barrow";
})

Upvotes: 0

Related Questions