Clément Andraud
Clément Andraud

Reputation: 9279

jQuery, select an element

I have this html :

<div id="para0104" class="entry">
<label for="id0104">d</label>
<div>
<label for="input0104100">
<input id="input0104100" type="radio" name="input0104" value="wxcwxc" />
wxcwxc</label>
<label for="input0104100">
<input id="input0104100" type="radio" name="input0104" value="wc" />
wc</label>
<label for="input0104100">
<input id="input0104100" type="radio" name="input0104" value="az" />
az</label></div>
</div>

I am in a loop for each input type radio, so $(this) return to me these inputs.

How can i retrieve the value "d" in <label for="id0104">d</label> ?

When i try $(this).parent().parent(); i retrieve <div>, and when i try $(this).parent().parent().parent(); i retrieve <div id="para0104" class="entry">

thanks :)

Upvotes: 0

Views: 149

Answers (2)

Murali Murugesan
Murali Murugesan

Reputation: 22619

Try using closest method

$(this).closest('div.entry').find('label:first').text()

Upvotes: 0

Konstantin Dinev
Konstantin Dinev

Reputation: 34915

You can use jQuery closest and select the label child:

var txt = $(this).closest('div.entry').children('label').text();

Upvotes: 2

Related Questions