James Radford
James Radford

Reputation: 1855

how to get radio button group selection

I have the following code although is there a more elegant way to getting the selected radio value as I'm already inside the code for the radio button group. This is to get the selected radio button within a group. Many thanks,

$('input[name=pig]').change(function () {

    // this seems redundant
    namespace('bla').myVariable = $('input[name=pig]:checked').val();  

    // ie. something like
    namespace('bla').myVariable = $(this).checked.val();
});

Upvotes: 0

Views: 61

Answers (3)

xdazz
xdazz

Reputation: 160833

$(this).val() is just enough:

$('input[name=pig]').change(function () {
  console.log($(this).val()); // or this.value
});

The demo.

Upvotes: 0

Janak
Janak

Reputation: 5052

This should do it.

  namespace('bla').myVariable = $(this).val();

Upvotes: 1

billyonecan
billyonecan

Reputation: 20250

I think this is what you're trying to achieve:

namespace('bla').myVariable = this.value;

Upvotes: 1

Related Questions