0

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... }); } 
2
  • 1
    @PabloFernandez: your code won't pass jslint you need to use === Commented Jul 29, 2011 at 13:28
  • @qwertymk so true. But as Brendan Eich himself said: "jslint can suck it" Commented Jul 29, 2011 at 14:05

3 Answers 3

2
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.

Sign up to request clarification or add additional context in comments.

2 Comments

yep no need for the loop just grab the checked radio.
$('div[id=' + tempId + ']') will always be true! :)
1
var obj = null, tempId = "RB-01", $div = $('#'+tempId); if ($div.length){ obj = $div.find('input:radio:checked').val(); } 

Here is a demo

Comments

0

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!

2 Comments

Actually he wants the radio input that is checked. So this doesn't work.
I've edited my answer to suit - looks like I edited as as you wrote your comment!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.