H. Ferrence
H. Ferrence

Reputation: 8116

How to capture the label of a checked radio button in jquery

I have this block of markup:

<fieldset class="form__list form__inlineButtons" enabled="enabled">
<label for="paymentMethod_839838" class="form__optional">
<input type="radio" value="1" name="paymentMethod" id="paymentMethod_839838" class="form__list jq__pmt1" style="display: inline-block;"> Cash
</label>
<label for="paymentMethod_570709" class="form__optional">
<input type="radio" value="2" name="paymentMethod" id="paymentMethod_570709" class="form__list jq__pmt2" style="display: inline-block;"> Check
</label>
<label for="paymentMethod_692392" class="form__optional">
<input type="radio" value="3" name="paymentMethod" id="paymentMethod_692392" class="form__list jq__pmt3" style="display: inline-block;"> Credit Card
</label>
<label for="paymentMethod_567739" class="form__optional">
<input type="radio" value="4" name="paymentMethod" id="paymentMethod_567739" class="form__list jq__pmt4" style="display: inline-block;"> Invoice
</label>
<label for="paymentMethod_227750" class="form__optional">
<input type="radio" value="5" name="paymentMethod" id="paymentMethod_227750" class="form__list jq__pmt5" style="display: inline-block;"> Payment Pending
</label>
</fieldset>

I have this jquery:

$('[name="paymentMethod"]').change(function() {
alert( $('[name="paymentMethod"] :radio:checked + label').text() );
}); // end .change()

I can't seem to capture the label of the checked radio button

Upvotes: 0

Views: 242

Answers (1)

Jay Blanchard
Jay Blanchard

Reputation: 34416

You would do it like this -

$('[name="paymentMethod"]').change(function() {
    var label = $(this).closest('label').text();
    alert(label);
}); // end .change()

http://jsfiddle.net/MBz4k/

Upvotes: 1

Related Questions