I have a form where each row has a name and the following columns have the answer choices. I’m having a problem where if anything on choice4 or choice5 is the first to be clicked in the entire form, the warning pops up. It works fine otherwise. This is my function as I tried to repeat it for all my other rows. Should I instead opt for a loop since I want to repeat the warning for every row? Not sure which type to use, in all honesty.
<tr class="candidates">
<td>Marie Antoniette</td>
<td> <input type="radio" value="mAnton" name="choice1" onchange ="sameCand()"></td>
<td>
<input type="radio" value="mAnton" name="choice2" onchange ="sameCand()">
</td>
<td>
<input type="radio" value="mAnton" name="choice3" onchange ="sameCand()">
</td>
<td>
<input type="radio" value="mAnton" name="choice4" onchange ="sameCand()">
</td>
<td>
<input type="radio" value="mAnton" name="choice5" onchange ="sameCand()">
</td>
</tr>
This is my function
function sameCand() {
let x = document.forms["ballot2"]["choice5"].value;
let y = document.forms["ballot2"]["choice4"].value;
let z = document.forms["ballot2"]["choice3"].value;
let a = document.forms["ballot2"]["choice2"].value;
let b = document.forms["ballot2"]["choice1"].value;
if (x == y || x==z || x==a || x==b) {
alert("Warning!Each candidate can only be ranked once. ");
return false;
}
else if (y == z || y==a || y==b) {
alert("Warning!Each candidate can only be ranked once. ");
return false;
}
}