pastx
pastx

Reputation: 113

Dynamic jQuery selector

I decided to change my question to make it easyer to understand.

I generate form from db using php. It is quiz, it has n(number) of groups of radio buttons, each group represents answers of 1 question in my quiz, name of each group is name1, name2, name3 ... depends on number of questions and answers in database.

I need to select only one group of radio buttons (one question and answers in quiz) and get value of it and then send it through ajax to be compared with right anwer in database.

So i need selector that allows me to do this, question after question to the end of the quiz.

This is my HTML code generated from DB:

<form>
<fieldset>
<label for = "otazka1">Kolik je 1+1 ?</label>
<div id="radio">
<input type="radio" id="radio1" name="0" value="0" /><label for="radio1">4</label>
<input type="radio" id="radio2" name="0" value="1" /><label for="radio2">2</label>
<input type="radio" id="radio3" name="0" value="2" /><label for="radio3">3</label>
</div>
<label for = "otazka2">Kolik je 2+2 ?</label>
<div id="radio">
<input type="radio" id="radio1" name="1" value="0" /><label for="radio1">2</label>
<input type="radio" id="radio2" name="1" value="1" /><label for="radio2">3</label>
<input type="radio" id="radio3" name="1" value="2" /><label for="radio3">5</label>
</div>
<label for = "otazka3">Kolik je 3+3 ?</label>
<div id="radio">
<input type="radio" id="radio1" name="2" value="0" /><label for="radio1">5</label>
<input type="radio" id="radio2" name="2" value="1" /><label for="radio2">10</label>
<input type="radio" id="radio3" name="2" value="2" /><label for="radio3">9</label>
</div>
</fieldset>
</form>

Upvotes: 1

Views: 106

Answers (2)

parliament
parliament

Reputation: 22904

You can concatenate your index to the selector to get the input with the correct index

for(i=0;i<numberOfInputs;i++){
    $("form input:eq(" + i + ")").getVal()
}

In your example it wouldn't really make sense to use a number as a class so your question is a little unclear.

Upvotes: 0

Dom
Dom

Reputation: 40461

I think I understand what you are asking. Use each() to iterate through inputs.

Your current code: $("input.i") is looking for inputs that have the class i.

$('input').each(function(){
    $(this).val();
});

Upvotes: 2

Related Questions