Tara Samons
Tara Samons

Reputation: 165

Call a Variable inside HTML with JQuery

I want to loop through radio button values and add them up for a quiz. My question is how to call a variable for "name" inside this statement:

   answer = $("input[type='radio'][name='q1']:checked");

I need to increment q1 by 1 in my loop. I thought I could use my variable q for this, just not sure how to stick it in there:

   for (var j=1; j < 11; j++) 
   {
     q="q"+j;
     answer = $("input[type='radio'][name='q1']:checked");
     answer = answer.val();
     answer = Number(answer);
     total = total + answer;
    }

Upvotes: 0

Views: 35

Answers (1)

john Smith
john Smith

Reputation: 17906

use concatination like :

answer = $("input[type='radio'][name='"+q+"']:checked");

Upvotes: 2

Related Questions