0

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;
    }

1 Answer 1

2

use concatination like :

answer = $("input[type='radio'][name='"+q+"']:checked");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.