Reputation: 131
HTML:
<span style="display:inline-block;width:250px;">
<div class="radio" id="uniform-rdo">
<span>
<input id="rdo" type="radio" name="lossDes" value="rdo" onclick="LossDes();" style="opacity:0; ">
</span>
</div>
<label for="rdo">Insured drove into water.</label>
</span>
jQuery:
var lossOptionsVal = $('input[name=lossDes]:checked').next('label').text();
Here now i want to get the text within label tag on check of radiobutton
Upvotes: 1
Views: 1122
Reputation: 1120
you can also do this way:
$('input:[type=radio]').click(function(){
var lossOptionsVal = $('label:[for='+$(this).attr('id')+']').text();
alert(lossOptionsVal);
});
Upvotes: 0
Reputation: 176906
Try
here as per you html label element is next to div element so you need to go two level up i.e. require to move to parent of parent which is div so the script is..............
var lossOptionsVal = $('input[name=lossDes]:checked')
.parent().parent().next('label').text();
Upvotes: 0
Reputation: 10305
I think you are looking for
$("#rdo").click(function() {
alert($(this).parents("div").next("label").text());
});
note that I'm using the jQuery click
method to bind the OnClick
event on the input, instead of defining in within the html.
Try it: http://jsfiddle.net/8d3gR/
Upvotes: 1
Reputation: 2356
Another solution
$("input[name=lossDes]").click(function() {
alert($(this).parents().eq(1).next().text());
});
and link: http://jsfiddle.net/jWB7t/2/
Upvotes: 0
Reputation: 3485
Here is a structure-agnostic, performance-light way to do it:
$('body')
.delegate('input[type="radio"]', 'click', function() {
var label_text = $('label[for="' + $(this).attr('id') + '"]').text();
// do whatever you need
});
Upvotes: 0