I have a form that is being validated once the user clicks submit. If there is an error, a <span>
tag with the class message
is displayed above each input field that is empty. When the user makes the correction and clicks submit again, I want all the message
classes to be removed that way I can validate the inputs again without having to worry about duplicating the error messages.
HTML
<div class="row">
<div class="column is-6--tablet">
<h2>Personal Information</h2>
<div class="row">
<div class="column is-3">
<span class="message">Please enter your first name.</span>
<label for="xFIRST_NAME">First Name</label>
</div>
<div class="column is-9">
<input type="text" name="xFIRST_NAME" value="<?php echo $xFIRST_NAME; ?>" required>
</div>
</div>
<div class="row">
<div class="column is-3">
<span class="message">Please enter your last name.</span>
<label for="xLAST_NAME">Last Name</label>
</div>
<div class="column is-9">
<input type="text" name="xLAST_NAME" value="<?php echo $xLAST_NAME; ?>" required>
</div>
</div>
<div class="row">
<div class="column is-3">
<span class="message">Please enter your phone.</span>
<label for="XPHONE">Phone</label>
</div>
<div class="column is-9">
<input type="tel" name="xPHONE" value="<?php echo $xPHONE; ?>" required>
</div>
</div>
<div class="row">
<div class="column is-3">
<label for="xEMAIL">Email</label>
</div>
<div class="column is-9">
<input type="email" name="xEMAIL" value="<?php echo $xEMAIL; ?>" required>
</div>
</div>
<div class="row patient-type">
<div class="column is-12">
<hr>
<input type="radio" name="xPATIENT_STATUS" id="NEW_PATIENT" value="New Patient" <?php echo ($xPATIENT_STATUS === 'New Patient' ? 'checked' : ''); ?> required>
<label for="NEW_PATIENT"> New Patient</label>
<input type="radio" name="xPATIENT_STATUS" id="EXISTING_PATIENT" value="Existing Patient" <?php echo ($xPATIENT_STATUS === 'Existing Patient' ? 'checked' : ''); ?>>
<label for="EXISTING_PATIENT"> Existing Patient</label>
</div>
</div>
</div>
</div>
JavaScript
var messages = document.querySelectorAll('.message');
for(var i = 0; 1 < messages.length; i++)
{
messages[i].parentNode.removeChild(messages[i]);
}
The error I am getting says TypeError: messages[i] is undefined
How can I query all the classes message
and remove them?I have seen similar questions on here but none of them seem to answer what I am looking for.