duduklein
duduklein

Reputation: 10604

Jquery: Get row from cell value

I have a table where all my rows are like this:

<tr> 
    <td class="name">name1</td>
    <td class="type">type1</td>
    <td class="edit">
        <a href="edit" class="edit">edit</a>
    </td>
</tr>

I need to disable the edit href for certain types. So I need something like:

row = $('.mytable').find(row where .type value = type_with_no_edit) #this is where I need elp
row.find('a.edit').remove();

If the row was alwasy the first one, I would do:

row = $('.mytable tbody>tr:first')

but that's not always the case.

Upvotes: 1

Views: 1735

Answers (2)

David Hellsing
David Hellsing

Reputation: 108482

You can use the :contains pseudo-selector:

$('td.type:contains("type_with_no_edit")').siblings('.edit').empty();

Demo: http://jsfiddle.net/XLwfs/

If you don’t want to empty the TD, just target the anchor and remove it instead.

Upvotes: 2

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

Sounds like a job for filter...

$('.mytable tr').filter(function() {
    return $("td.type", this).text() === "type_with_no_edit";
}).find('a.edit').remove();

That finds every row that has the text type_with_no_edit in the td with the class type and removes the td with a.edit that is a sibling.

Upvotes: 4

Related Questions