views:

218

answers:

4

as said in the title for example:

<input id="User_Type_0" type="radio" name="User_Type" value="1" checked="checked" />
<label for="User_Type_0">User1</label>
<input id="User_Type_1" type="radio" name="User_Type" value="2" />
<label for="User_Type_1">User2</label>

how can I get the text:User 1

+7  A: 

$('input:radio:checked').siblings('label:first').html()


UPDATE:

As pointed out by Victor in the comments section the previous selector will always select the first label. The next function should work:

$('input:radio:checked').next('label:first').html()
Darin Dimitrov
and if the order of the html-elements changes, you are screwed :-)
Natrium
And this always selects the first label in DOM order, not the next one!!
Victor
@Victor, you are correct. Thanks for pointing this out. I've updated my answer.
Darin Dimitrov
You are welcome!
Victor
+3  A: 

how about this?

var forLabel = $('input:radio:checked').attr("id");
$("label[for='" + forLabel + "']").text();
Natrium
Good general idea, but you'll want to use a different variable name – `for` is a reserved word.
bobince
if there're many groups, how can I get what I want?
Michael
changed variable-name.
Natrium
if there are more than 1 group, you can select which group you need with the `name`-attribute of the radiobutton
Natrium
+1  A: 

use .next();

$("input:radio:checked").next().text();
Reigel
A: 

What about using the next Adjacent Selector, +?

$('input:radio:checked + label').text();

Here's a Working Demo

Russ Cam