Struggling with a simple code here, trying to figure out how to add color (class) to the selected radio button and the previous ones. For example, if button No3 is selected, buttons 1, 2, and 3 should get a new color.
Below is the basic code snippet:
<form>
<ul>
<li>
<input id="radio1" type="radio" name="radio[]" value="1">
<label for="radio1">one</label>
</li>
<li>
<input id="radio2" type="radio" name="radio[]" value="2">
<label for="radio2">two</label>
</li>
<li>
<input id="radio3" type="radio" name="radio[]" value="3">
<label for="radio3">three</label>
</li>
<li>
<input id="radio4" type="radio" name="radio[]" value="4">
<label for="radio4">four</label>
</li>
<li>
<input id="radio5" type="radio" name="radio[]" value="5">
<label for="radio5">five</label>
</li>
</ul>
Attempted using .prevAll() but it didn't work as expected.
Here's the current script that adds class to all buttons:
$(document).ready(function(e) {
$("input[type=radio]").click(function(){
$('form ul li input[type=radio] + label').addClass('abc');
})
});
See demo here.
Should I consider using a loop? Any assistance would be highly appreciated.