2
votes

I got multiple radio groups with the same name and an id number in it. (group1, group2, group3...) to get one value:

var value = $('input:radio[name="group1"]:checked').attr("value");

If i know the id number of the group where I want to get the checked attribute how do i get it?

var i = 2;
var value = $('input:radio[name="group" + i]:checked').attr("value");

not working.

2

2 Answers

2
votes

i is a variable, you cant put it in the string directly.

Use string concatenation to concat the i with the string segments:

$(':radio[name="group' + i + '"]:checked').val();

Demo: http://jsfiddle.net/Pz66U/

Note: You can select :radio without using :input

1
votes

Try to do the concatenation properly,

var i = 2;
var value = $('input:radio[name="group"' + i +']:checked').val();

And to retrieve the value, you can use .val()