Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set asp radiobuttonlist selected item in jQuery

I have two radio button lists. I want to set the selected item in jQuery when the document is fully loaded. I have function that seems to set the checked value, but doesn't actually change what shows up on the screen:

function setRadioButtonListSelected(rbl, match) {
for (var i = 0; i < rbl.length; i++) {
    if (rbl[i].value == match) {
        rbl[i].checked = true;
        break;
    }
}
}

I know how to read the value of the rbl, but I haven't been able to find any examples of how to set the selected value so that the value already rendered changes based on the match value.

like image 618
Bob Jones Avatar asked Jan 25 '26 13:01

Bob Jones


1 Answers

This code worked for me.

Because the radio button list renders a bunch of controls you want the jquery selector to get the radio buttons only and filter out the selected radio button by the value by setting the checked property

    $( function () {
       var v = 1; // You would probably pass the selected value to a function instead 
       $('#radioButtonList[type=radio][value=' + v + ']').prop('checked', true);
    } );
like image 67
Orry Avatar answered Jan 28 '26 06:01

Orry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!