I am new to coding and I am trying to figure out how to make my code run when the variable i increases.
The current code executes when i = 1, as defined (.q1). The code also increases the variable i to 2, however, on the next click for .q2, the code doesn't run and I'm not sure why.
Can anyone help explain why the code doesn't run on the next set of buttons (i=2)?
var score = 0
var i = 1
$('.q' + i).on('click', function() {
if ($(this).is("#correct"))
$(this).addClass("theright") && ++score;
else {
$(this).addClass("thewrong");
}
$('.q' + i).attr('disabled', 'disabled') && ++i;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td><button class=q1 id=correct type="button">option 1</button></td>
<td><button class=q1 type="button">option 2</button></td>
</tr>
<tr>
<td><button class=q1 type="button">option 3</button></td>
<td><button class=q1 type="button">option 4</button></td>
</tr>
</table>
<table>
<tr>
<td><button class=q2 type="button">option 1</button></td>
<td><button class=q2 type="button">option 2</button></td>
</tr>
<tr>
<td><button class=q2 id=correct type="button">option 3</button></td>
<td><button class=q2 type="button">option 4</button></td>
</tr>
</table>