How to get the value of a textbox if a specific radio button is selected

02nd, January 2018 By

Sometime there may be condition when you have radio button with same name and you have text box with all the radio buttons you have to fetch text box value whose id match with the checked radio button’s value . For this you have radio buttons with the text boxes given as follows:

<td colspan="3">
<input id="first" type="radio" checked="checked" name="mymedicine" value="IV" />
 I / V <input class="select1" id="IVdose" style="width: 97px;" type="text" name="textfield8" />

 <input id="second" type="radio" name="mymedicine" value="IA" />
 I / A <input class="select1" id="IAdose" style="width: 97px;" type="text" name="textfield8" />
 <input id="third" type="radio" name="mymedicine" value="bridging" />
 Bridging <input id="bridgingdose" style="width: 97px;" type="text" name="textfield8" /></td>

Here you can see that we are having radio buttons with name mymedicine and its value match with its corresponding text box by combining dose text with its we have IV as value and we have IVdose id of text box .

<script type="text/javascript">
    var mymedicine= $("input:radio[name=mymedicine]:checked").val();
    var mymedicinedose=mymedicine+'dose';
    var mymedicine_dose_text=$('#'+mymedicinedose).val();

Here mymedicine is the name of radio buttons with script we will fetch checked radio button’s value and by combining dose text with it we fetch the text box value .

Leave a Reply

Your email address will not be published. Required fields are marked *