I have a select option like this:
<select name="color" id="multipleAttr" class="form-control select-2">
<option value="blue">
<option value="red">
</select>
Now in order to return the value of this with jQuery, I did this:
$(".select-2").change(function() {
var val = $(".select-2").val();
console.log(val);
});
It works fine and properly shows the value of selected item.
But I wanted to make this more dynamically by using a for loop:
var counters = 2;
for(i=2;i<=counters;i++){
$(".select-"+i).change(function() {
var val = $(".select-"+i).val();
console.log(val);
});
}
So it should be selecting select-2
class value and returns it as val
.
But it prints undefined somehow.
So what's going wrong here? How can I solve this issue?