Emerson F
Emerson F

Reputation: 845

How to get Text of radio button instead of value using jQuery?

This is the code generated by the software

<div id="cred_form_3584_1_wpcf-ticket-month-or-course-radios" class="myzebra-radios">
  <div class="myzebra-radios-single">
    <label class="myzebra-style-label">
      <input id="cred_form_3584_1_wpcf-ticket-month-or-course_wpcf-fields-radio-option-a6da15467aeb84f539c0dc1cd766ccd6-2" class="myzebra-control myzebra-radio myzebra-prime-name-wpcf-ticket-month-or-course" type="radio" checked="checked" value="wpcf-fields-radio-option-a6da15467aeb84f539c0dc1cd766ccd6-2" name="wpcf-ticket-month-or-course">
      <span class="myzebra-radio-replace"></span>
    </label>
    <label id="cred_form_3584_1_label_wpcf-ticket-month-or-course_wpcf-fields-radio-option-a6da15467aeb84f539c0dc1cd766ccd6-2" for="cred_form_3584_1_wpcf-ticket-month-or-course_wpcf-fields-radio-option-a6da15467aeb84f539c0dc1cd766ccd6-2">per month</label>
  </div>
<div class="myzebra-radios-single">
  <label class="myzebra-style-label">
    <input id="cred_form_3584_1_wpcf-ticket-month-or-course_wpcf-fields-radio-option-f63eb739e2a6499a882c8e82aa35b028-1" class="myzebra-control myzebra-radio myzebra-prime-name-wpcf-ticket-month-or-course" type="radio" value="wpcf-fields-radio-option-f63eb739e2a6499a882c8e82aa35b028-1" name="wpcf-ticket-month-or-course">
    <span class="myzebra-radio-replace"></span>
  </label>
  <label id="cred_form_3584_1_label_wpcf-ticket-month-or-course_wpcf-fields-radio-option-f63eb739e2a6499a882c8e82aa35b028-1" for="cred_form_3584_1_wpcf-ticket-month-or-course_wpcf-fields-radio-option-f63eb739e2a6499a882c8e82aa35b028-1">per course</label>
</div>

Using this code:

alert($('input[@name="myzebra-radios-single"]:checked').text());

I get the value but not the text. How do I get the text (e.g per month) instead?

Upvotes: 0

Views: 7646

Answers (3)

Blazemonger
Blazemonger

Reputation: 92893

To get the contents of the label, you need to use its for= attribute:

var $input = $('input[name=something]:checked');
var text = $('label[for='+$input.attr('id')+']').text();

http://jsfiddle.net/mblase75/RDPP7/

Upvotes: 3

polkduran
polkduran

Reputation: 2551

try :

alert($('input[@name="myzebra-radios-single"]:checked').attr('name'));

Upvotes: 0

Eric Herlitz
Eric Herlitz

Reputation: 26267

First of all the radio elements doesn't have any text values, if they did however.

alert($('input[@name="myzebra-radios-single"]:checked').text());
// Should be
alert($('input[@name="wpcf-ticket-month-or-course"]:checked').text());

When I breakdown your inputs they look something like this

<input 
    id="cred_form_3584_1_wpcf-ticket-month-or-course_wpcf-fields-radio-option-f63eb739e2a6499a882c8e82aa35b028-1" 
    class="myzebra-control myzebra-radio myzebra-prime-name-wpcf-ticket-month-or-course" 
    type="radio" 
    value="wpcf-fields-radio-option-f63eb739e2a6499a882c8e82aa35b028-1" 
    name="wpcf-ticket-month-or-course">

If you want to pick a certain attribute from the above the use either .val() .text() or .attr().

Upvotes: 0

Related Questions