Bonik
Bonik

Reputation: 812

Get the above tr in a table with a specific class

What i'm trying to do is: i'm having a table that looks like this

<table>
    <tr class="warning">
        <input type="hidden" name="ID[]" value="1" />
        <td>warning</td>
    </tr>
    <tr>
        <td>
            <a href="#" class="editField">edit</a>
        </td>
    </tr>
    <tr>
        <td>
            <a href="#" class="editField">edit</a>
        </td>
    </tr>
    <tr>
        <td>
            <a href="#" class="editField">edit</a>
        </td>
    </tr>
    <tr class="warning">
        <input type="hidden" name="ID[]" value="2" />
        <td>warning</td>
    </tr>
    <tr>
        <td>
            <a href="#" class="editField">edit</a>
        </td>
    </tr>
</table>

And for each warning tr there is an hidden input with specific ID. I've tried:

$.each($('a.editField'), function(i,obj){
   console.log($(obj).parent().parent().prevAll('tr.warning').find('[name="ID[]"]').val());
});

But it's logging me the first warning tr in the table, so the log will be four times ID 1, and what it should be is three times ID 1 and one time ID 2.

JsFiddle: http://jsfiddle.net/dLp7b/

Upvotes: 0

Views: 38

Answers (1)

Omri Aharon
Omri Aharon

Reputation: 17064

You're just missing :first. That would give you the first closest sibling.

$.each($('a.editField'), function(i,obj){
    console.log($(obj).parent().parent().prevAll('tr.warning:first').find('[name="ID[]"]').val());
});

Demo

Upvotes: 1

Related Questions