tags:

views:

89

answers:

4

Is there a way to use a variable in the name= parameter.

I would like to be able to do:

var a = 1;

$("#gen_p").html($("input:radio[name='gen'+a]:checked").val()));

I am able to do $("#gen_p"+a) but not in the [name=??]

Have I missed something?

Thanks

+8  A: 
$("#gen_p").html($("input:radio[name='gen"+a+"']:checked").val());​​

You're mixing some single quotes in there.

EDIT: You were also having some extra ) in there.

Ben
Heads up to the OP: this one was first.
Samir Talwar
@Samir - Why do you care? Maybe Sarfraz would have been first if he wasn't taking the time to edit the question for the OP.
patrick dw
I can't see extra `)` there in his code.
Sarfraz
@Me-and-Coding - He has `$().html($().val()));`, that's one extra `)` in there. It should be `$().html($().val());` <- `.html()` closing `)`
Ben
@Me-and-Coding - Easy way to tell, count all of them. There are 9. Should be an even number.
patrick dw
Thanks to all. I thought I had tried that, but obviously not.
Ian
Why was this answer down-voted twice? @Ben gave a correct solution.
patrick dw
@Ben: Voted up again, thanks for clarification on closing brace.
Sarfraz
@patrick: I figure it's useful information. I don't see the need for multiple answers that say the same thing, and unfortunately, I can't find a better measure to determine which one should be the "correct" one than determining which one went first. If the OP chooses to go by some other criteria when picking the right answer, he's welcome to.
Samir Talwar
@Samir - With all due respect, the quality of the code and explanation may be a factor as well. It is for me. You are influencing the OP by suggesting that an arbitrary standard of speed ought to take the highest precedence. There are some high-score users here that get their answer in first because they offer a very short sentence to begin, then edit their answer. Edits don't show up when a user edits their own post within 5 minutes, or so. It is a fair tactic, but becomes less fair when others suggest that "first post" should be the primary consideration.
patrick dw
@patrick: I really dislike the way people post really short answers then edit later. The reason I commented was because at the time, there were three answers and they were all exactly the same. I completely agree that an answer of a higher caliber should be preferred, but when they're essentially duplicates, it's not very helpful.
Samir Talwar
@Samir - Agreed. Rock on! :o)
patrick dw
+3  A: 

Yes, you can do that, but you put single quotes within the double quotes, so it's reading it literally as "'gen'+a", not "gen" + a.

Try this:

$("#gen_p").html($("input:radio[name=gen" + a + "]:checked").val()));
Kerry
+2  A: 

You are missing double quotes as well as + operator there:

var a = 1;
$("#gen_p").html($("input:radio[name='gen'" + a + "]:checked").val()));

In javascript, variables should not come inside single/double quotes, rather they should be put outside of them and separated with + operator (which is also concatenation operator in javascript).

Sarfraz
+1  A: 

When quotes get messy, I prefer using string replace.

var selector = "input:radio[name='gen{a}']:checked".replace("{a}", a);

$("#gen_p").html($(selector).val());
Anurag