I'm trying to add a class to a specific instance of another class, while modifying the name of the class. This is the code i have:
for(var i = 0; i<3; i++){
$('.formEntry')[i].addClass("form" + i);
}
This is the code before the function is ran:
<fieldset class = "formEntry"></fieldset>
<fieldset class = "formEntry"></fieldset>
<fieldset class = "formEntry"></fieldset>
and this is the desired output.
<fieldset class = "formEntry form1"></fieldset>
<fieldset class = "formEntry form2"></fieldset>
<fieldset class = "formEntry form3"></fieldset>
Please help!
$('.formEntry')[i]returns the native DOM element, which doesn't have an addClass function. You could either do$('.formEntry').eq(i)or$($('.formEntry')[i])to get the proper jQuery object that you can call addClass on. (the former is probably better)