I have a form which looks like this:
<input type="radio" id="answer0" name="answer0" value="1" />
<input type="radio" id="answer0" name="answer0" value="2" />
<input type="radio" id="answer0" name="answer0" value="3" />
<input type="radio" id="answer0" name="answer0" value="4" />
What I want is to get the selected radio button so I used the jQuery code below.
var id = "#answer0";
var isChecked = $(id).prop('checked');
But I only get a result when the first radio button is checked. No result if I select the second, third and fourth one. Any help please.
Oh sorry. I generated the radio button in php like this...
print '<input type="radio" class="answer'. $q . '" id="answer'. $q . '" name="answer'. $q . '"value="1"/>' . $row['answer1'] .'<br/>';
print '<input type="radio" class="answer'. $q . '" id="answer'. $q . '" name="answer'. $q . '"value="2"/>' . $row['answer2'] .'<br/>';
print '<input type="radio" class="answer'. $q . '" id="answer'. $q . '" name="answer'. $q . '"value="3"/>' . $row['answer3'] .'<br/>';
print '<input type="radio" class="answer'. $q . '" id="answer'. $q . '" name="answer'. $q . '"value="4"/>' . $row['answer4'] .'<br/>';
so I'm sure that the id is unique. What I am trying to do is get the selected radio button value for answer0, answer1 and so on... So far I can only get the first group value (answer0).
my jquery code:
for ( var i = 1; i <=items; i++ ) {
var t = "answer" + i;
if($('.' + t).prop('checked')){
//alert(this.id);
alert(t);
correct++;
}
alert(correct);
}}
idmust be unique, no ifs, no buts; a repeatedidis invalid HTML; you should be using aclass.