user1093452
user1093452

Reputation: 131

I want to get the label text on check of radio button

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

Answers (5)

Punit
Punit

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

Pranay Rana
Pranay Rana

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

Manuel van Rijn
Manuel van Rijn

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

Mr.T.K
Mr.T.K

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

Jason
Jason

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

Related Questions