Summary:
I want to create a self-written form that checks the correctness of the user input, that is, whether the user filled the field or not, in the case of a single empty field, the user input highlighted with a red frame
Task: There is a certain condition in the source that checks with arrInput[i].value.length == 0 , but the problem is that when I click the send button, the class specified by arrInput[i].classList.add("form__item"); at the moment disappears . I had to stop the script via alert ();
Source:
let name = document.querySelector('form input[name=name]'); let lastName = document.querySelector('form input[name=lastName]'); let age = document.querySelector('form input[name=age]'); let send = document.querySelector('form input[type=submit]'); let arrInput = [name, lastName, age]; send.addEventListener('click', checkOut); function checkOut() { for (let i=0 ;arrInput.length > i; i++ ){ if (arrInput[i].value.length == 0){ arrInput[i].classList.add("form__item"); alert(1); } } } .form .form__item.input[type=text] { border: 1px solid red; } <form class="form" action=""> <input type="text" name="name"><br> <input type="text" name="lastName"><br> <input type="text" name="age"><br> <input type="submit" value="send"> </form>