Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display the text label for selected radio button in Jquery

Tags:

jquery

Hai,

I have a group of radio buttons, i could able to pick the selected value using jQuery but not the text label for 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>

JQUERY CODE TO PICK THE SELECTED VALUE

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

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

Now my requirement is, i would like to display the label of the selected values for eg (high or low or medium) depends on the selection of the radio button.

Hope this helps. let me know if you have any question. Kindly help me in this task

like image 797
Thinker Avatar asked Mar 23 '10 11:03

Thinker


People also ask

How do I get the selected radio button label?

You can simply use the jQuery :checked selector in combination with the val() method to find the value of the selected radio button inside a group.

How do you display a textbox when a radio button is selected?

Add your text box inside of a DIV element. Hide the div on page load. Register a java script on click for checkbox list. If checkbox selected value is Other means, make visible that DIV element.

How can I know which radio button is selected via jQuery?

To check which radio button is selected in a form, we first get the desired input group with the type of input as an option and then the value of this selection can then be accessed by the val() method. This returns the name of the option that is currently selected.

Does a radio button need a label?

A related group of radio buttons must have a grouping label. Radio buttons that provide a set of related options need grouping information and a common grouping label to provide the overall context for those options.


1 Answers

My first thought was that the text() will work correctly. Unfortunately there is no innerText for radio. You can use a label along with the radio and specify the for attribute as the radio button id. Something like

.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> 

See a working demo

like image 147
rahul Avatar answered Oct 20 '22 00:10

rahul