views:

2098

answers:

2

Hi all,

I was wondering if anyone can post an example of how to get aselected radio button option from an asp.net radio button list control via jquery on the loading of a page.

Thanks

+1  A: 

In your javascript function where you want to query the list, use this code..

var selected = jQuery('#<%= MyRadioButtonList.ClientID %> input:checked').val();
// or ...
var selected = $('#<%= MyRadioButtonList.ClientID %> input:checked').val();

to set a sample label with the results of your selected radiobuttonlist, you could do this...

$(document).ready(function(){
    var selected = $('#<%= MyRadioButtonList.ClientID %> input:checked').val();
    $("#<%= MySampleLabel.ClientID %>").text(selected);
}
Scott Ivey
...assuming that SearchOptions is the control in which the radio buttons are rendered.
DDaviesBrackett
correct - updated example to try to clarify.
Scott Ivey
Thanks scott. This works perfectly.
zSysop
A: 

Working example here.

The selector I used to get the radio buttons will grab all the radio buttons with the class ofinterest on the page.

$(function(){ 
  var value = $('input.ofinterest:checked').val();
  $('#result').text(value); 
});

If you want to scope the selector further, and you don't mind writing your JS directly in your aspx/ascx, you can use Scott's solution above instead. But if you give the buttons you're interested in a known classname, you can put this JS in a .js file.

DDaviesBrackett