aw crud
aw crud

Reputation: 8891

How can I find out which radio button is selected using jQuery without searching for the radio group each time?

I know I can use something described here: How can I know which radio button is selected via jQuery?

i.e. jQuery("input[name=myradiogroup]:checked").val() to get the selected radio button value. But I'd like to cache the radio group and determine which value is selected at a later point in time.

I want to do something like:

var myRadio = jQuery("input[name=myradiogroup]");
//some code
var value = myRadio.getCheckedButton().val();

Any way to do this or do I have to explicitly run the selector with :checked in it every time I want to find out the selected value?

Upvotes: 6

Views: 2015

Answers (3)

Mark Schultheiss
Mark Schultheiss

Reputation: 34227

myValue="";
$('input[name=myradiogroup]').change(function() {
     myValue= this.value;
    alert(myValue);
});

Now you can check "myValue" anytime you wish.

Upvotes: -1

epascarello
epascarello

Reputation: 207557

var myRadio = jQuery("input[name=myradiogroup]");
var selectedRadio = myRadio.filter(":checked");
alert( selectedRadio.val() );

Upvotes: 8

Anurag
Anurag

Reputation: 141909

Could do

myRadio.filter(':checked').val()

Upvotes: 4

Related Questions