RaiBnod RaiBnod - 4 months ago 9
jQuery Question

Getting active label name in HTML

I have following lines of code:

<div>
<label>Property type</label>

<div class="btn-group btn-group-justified" data-toggle="buttons">
<div class="btn-group">
<label class="btn btn-default">
<input name="FIRST" value="2011" type="radio">HDB
</label>
</div>

<div class="btn-group">
<label class="btn btn-default active">
<input name="SECOND" value="2012" type="radio">Condo / Apt
</label>
</div>

<div class="btn-group">
<label class="btn btn-default">
<input name="THIRD" value="2013" type="radio">Landed
</label>
</div>
</div>
</div>


Now my question is: How can i get the name of input tag which is highlighted as active in label (From JavaScript or JQuery).

In above scenario answer should be: SECOND

Answer

If there is only one active label :

$('.active').find('input').attr("name");

Thanks to JeetDaloneboy for providing this JSFiddle example here.

Comments