Display text label for selected radio button in jQuery

Hi

I have a group of radio buttons, I can select the selected value using jQuery, but not a text label for the selected values.

for ex:

<input type="radio" value="1" name="priority">High</input>
<input type="radio" value="2" name="priority">Medium</input>
<input type="radio" value="3" name="priority">Low</input>

ABBREVIATION CODE FOR CHOOSE A SELECTED VALUE

jQuery('input:radio[name=priority]').change(function()
{
 var priority_type=jQuery(this).attr("value");
        alert(priority_type);
}

OUTPUT will be any of the following (1,2,3)

Now my requirement: I would like to display the label of the selected values, for example, (high, low or medium) depends on the choice of the switch.

Hope this helps. let me know if you have any questions. Please help me in this task.

+5
source share
1 answer

, () . , . for . -

. text()

jQuery('input:radio[name=priority]').change(function()
{
   var id = $(this).attr("id");
   alert($('label[for='+id+']').text());
}

<input type="radio" name="priority" value="1" id="rdLow" />
<label for="rdLow">Low</label> 
<input type="radio" name="priority" value="2" id="rdMedium" />
<label for="rdMedium">Medium</label> 
<input type="radio" name="priority" value="3" id="rdHigh" />
<label for="rdHigh">High</label> 

.

+6

All Articles