I need to add the class error to a <label> with the name attribute set to choice_16_0. The code I wrote to do this, however, changes every label on the page to <label for="choice_16_0" class="error">, instead of just adding the new class to the label that already contains for="choice_16_0".
Here’s my code:
if (!$("input[@name=\"choice_16_0\"]:checked").val()) {
$("label").attr("for","choice_16_0").addClass("error");
};
What’s wrong with this, and how can I fix it?
You selector
$("label")is what’s selecting every label on the page, change it to$("label[name=choice_16_0")to have it select the single label with the name you want.