Darien Fawkes
Darien Fawkes

Reputation: 119

How to get value of a selected radio button?

I have a <div> with radioboxes:

<div id='RB-01'>
<span>Item_1</span><input type='radio' name='RB01' value='1'><br />
<span>Item_2</span><input type='radio' name='RB01' value='2'><br />
<span>Item_3</span><input type='radio' name='RB01' value='3'><br />
</div>

Then with jQuery I want to get number on an account a radiobox that was checked:

var obj;
var tempId = "RB-01";
if ($('div[id=' + tempId + ']')) {
  $('div[id=' + tempId + '] input').each(function() {

  ...here I need save in the variable obj the number on an account input that was checked... 

  }); 
}   

Upvotes: 0

Views: 238

Answers (3)

Shef
Shef

Reputation: 45589

var obj = null,
    tempId = "RB-01",
    $div = $('#'+tempId);

if ($div.length){
    obj = $div.find('input:radio:checked').val();
}

Here is a demo

Upvotes: 1

Stuart.Sklinar
Stuart.Sklinar

Reputation: 3761

Sounds like you need

  $('div[id=' + tempId + '] input:checked').each(function() {

  var number = $(this).val();

  }); 

You could have quite easily found this on the jQuery API site...

JQuery each!

Upvotes: 0

tskuzzy
tskuzzy

Reputation: 36446

var obj = null;
var tempId = "RB-01";

if ($('div[id=' + tempId + ']')) {
  obj = $('#' + tempId + ' input:checked').val();
}

There's no need to use the div[id=RB-01] selector since IDs are unique. Just use #RB-01. Then use the :checked selector to get the checked radio button.

Upvotes: 2

Related Questions