serge
serge

Reputation: 15249

Select the closest (selector) element

I have the following:

<tbody id="tableBody">
    <tr>
        <td><input id="id" name="id" type="radio" value="1"></td>
        <td>Adam</td>
        <td>London</td>
    </tr>
    <tr>
        <td><input id="Radio1" name="id" type="radio" value="2"></td>
        <td>Steve</td>
        <td>New York</td>
    </tr>
    <tr>
        <td><input id="Radio2" name="id" type="radio" value="3"></td>
        <td>Jaques</td>
        <td>Paris, France</td>
    </tr>
    <tr>
        <td><input id="Radio3" name="id" type="radio" value="4" 
            checked="checked"></td>
        <td>Jora</td>
        <td>Chisinau</td>
    </tr>
</tbody>

In a jQuery I selected the checked element (where the value = 4), like this:

var selectedRadio = $('#tableBody').find('input:radio:checked')

now I want to

selectedRadio.closest('tr').remove();

but before removing I want to check the previous (value = 3) radio...

How can I search the previous radio having the selectedRadio element, in order to check it?

Upvotes: 0

Views: 59

Answers (2)

Pradeep
Pradeep

Reputation: 1272

var selectedRadio = $('#tableBody').find('input:radio:checked');
var $tr = selectedRadio.closest('tr');
var $prev_tr = $tr.prev().find('input:radio');
$tr.remove()

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388446

var $tr = selectedRadio.closest('tr')
$tr.prev().find('input[type="radio"]').prop('checked', true)
$tr.remove();

Upvotes: 1

Related Questions