Getting ASP.NET Radiobutton list Text using javascript not JQuery





I came accross a problem while I was trying to Get ASP.NET Radiobutton list Text using JQuery.

I used the JQuery method

$(‘#<%=RadioButtonList1.ClientID%>’).val();

but it was not returning selected value though same works for asp.net Dropdown list then I started searching over the internet but didn’t find any solution.

After searching for an hour I started looking for classic javascript to get the text of selected item in the Radio button list,
but all of the examples were giving me value of the selected item in the radio button list.

Onec I was struggling to get the text of selected item in asp.net dropdown list then found a solution which you can see in my post here
by combining the logic of this disablingenabling-aspnet-checkboxlist-items-using-javascritp
post with the code below I was able to find a solution to my problem.
Following code will give you the text of selected Item in an asp.net radio button list same also is true asp.net check box  list

function getRadioButtonText()
{
var elementId = ‘<%=RadioButtonList1.ClientID%>’;
var elementRef = document.getElementById(elementId);
var checkBoxArray = elementRef.getElementsByTagName(‘input’);

var checkedValue = ”;
var isChecked = false;

for (var i = 0; i < checkBoxArray.length; i++) {
if (document.getElementById(elementId + “_” + i.toString()).checked == true) {
var checkBoxRef = checkBoxArray[i];
var labelArray = checkBoxRef.parentNode.getElementsByTagName(‘label’);
checkedValue = labelArray[0].innerHTML;
alert(checkedValue);
Chekced = false;

break;
}
}

}

Please post a JQuery solution if you have .




Tags: , , , ,

15 Responses to “Getting ASP.NET Radiobutton list Text using javascript not JQuery”

  1. admin says:

    Java script code was not working so I’ve changed it, this is 100% working code. Sorry! for any inconvenience caused.

  2. Sutha says:

    jQuery solution: (for this problem)

    $(‘#’).find(“input[CHECKED]“).val()

  3. Sutha says:

    jQuery Solution:

    $(’#’).find(”input[CHECKED]“).val();

  4. Sutha says:

    instead of #…please use #

  5. Mahesh says:

    Excellent.Thanks a Lot

  6. Yosel says:

    like Sutha said:
    $(“#”).find(“input[CHECKED]“).val()

  7. Yosel says:

    like Sutha said:
    $(‘’).find(”input[CHECKED]“).val()

  8. Anti- Coder says:

    It saved my life

    Thanks a lot

  9. Lee says:

    the code is work…
    thanks for sharing this valuable code!

  10. Jyothi says:

    Hey, This solution works like a charm.
    Thanks a ton.
    Jyothi

  11. Trupti says:

    Hey thanks a lot for such a valuable code. I had left hopes…. Thanks a lot.

  12. John says:

    Life Saver. Thank you.

  13. Usman Khalid says:

    Thank you very much man. Great post. :)

  14. BindraBindu says:

    Hello Everyone,
    This is very nice article !!!!!!! really great.
    you can use javascript in same manner with simple html radiobutton, for example please check out the following links…..

    http://www.mindstick.com/Blog/233/Get%20checked%20radiobutton%20using%20JavaScript

    Thanks !!!!!!

Leave a Reply