Can I use a conditional if statement inside the for loop?

I have not practiced js, so decided to resume the practice, but they have forgotten the basics.
I have a few form fields in a variable allFormInputs.
And I need those fields where not filled in the values of the frame turns red.
The form is in the variable feedbackForm.

var feedbackForm = document.querySelector(".modal--feedback__feedback-form");
var allFormInputs = document.querySelectorAll(".modal--feedback__input");

feedbackForm.addEventListener("submit", function (evt) {
if (!userNameField.value || !userMailField.value || !userMessageField.value) {
evt.preventDefault();
validationFieldsRemove(allFormInputs);
validationFieldsValue(allFormInputs);
}

var validationFieldsRemove = function (fields) {
for (var i = 0; i => fields; i++) {
if (fields[i].classList.contains("modal-error")) {here I get an error that interpreter cannot read property classList. Why, is not clear.
fields[i].classList.remove("modal-error");
}
}
return fields;
};
March 23rd 20 at 19:39
1 answer
March 23rd 20 at 19:41
Solution
for (var i = 0; i => fields; i++) {

can still for (var i = 0; i < fields.length; i++) {?
perhaps) Even as it is a shame now. - Modesta.Leusch commented on March 23rd 20 at 19:44
@Modesta.Leusch, not <=but just < (less than), otherwise the last element in the loop will be empty. - Dario commented on March 23rd 20 at 19:47
@Dario, norm, thank you. - Modesta.Leusch commented on March 23rd 20 at 19:50
@Modesta.Leusch, if the decision is correct, mark it add-on. - Nicholaus_Schroeder commented on March 23rd 20 at 19:53
@Dario, good point, fixed - roberto.OConner commented on March 23rd 20 at 19:56

Find more questions by tags JavaScript