title: Form validation with duplicated warning statement in JavaScript
body: <p>I am working with a form validation using JavaScript. When submit the form, the warning statement would appear under the input tag. However, if I click again, the warning statement would be created again. I understand there is a method that we can use HTML to solve this issue. But I was wondering how to solve only using JavaScript.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const form = document.querySelector('.apply__form');
  
  
  form.addEventListener('submit', e =&gt; {
    e.preventDefault();
    const requireAnswers = document.querySelectorAll('.answer');
    for (let requireAnswer of requireAnswers) {
      if (requireAnswer.parentElement.classList.contains('required') &amp;&amp; !requireAnswer.value) {
        const warningStatement = document.createElement('p');
        warningStatement.classList.add('warning__message');
        warningStatement.innerText = `Please fill ${requireAnswer.previousElementSibling.innerText}`;
        requireAnswer.parentElement.appendChild(warningStatement);
      }
    }
  })</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>body, html {
    box-sizing: border-box;
    font-size: 16px;
  }
  
  .wrapper {
    width: 100%;
    background:#f0f0f0;
    padding: 30px 0;
  }
  
  .apply__form {
    border-top: 8px solid #085a23;
    margin: 0 auto;
    max-width: 645px;
    background: #fff;
    padding: 50px;
  }
  
  .form__title {
    font: normal normal bold 1.8rem "Microsoft JhengHei";
  }
  
  .event__info {
    margin: 2rem 0;
    font: normal normal normal 0.9rem "Microsoft JhengHei";
  }
  
  .event__info .event__place {
    margin-top: 0.5rem;
  }
  
  .notice {
    color: #e74149;
    font: normal normal normal 1rem "Microsoft JhengHei";
  }
  
  .notice::before {
    content: "*";
    padding-right: 5px;
  }
  
  .questions {
    width: 100%;
  }
  
  .question {
    font: normal normal normal 1rem "Microsoft JhengHei";
    margin-top: 50px;
  }
  
  .question .question__title {
    margin-bottom: 20px;
  }
  
  .question .question__title::after {
    content: "*";
    color: #e74149;
    padding-left: 5px;
  }
  
  .question:nth-child(4) .type1 {
    margin-bottom: 23px;
  }
  
  .question:nth-child(6) .question__title::after {
    content: none;
  }
  
  .sub__title{
    margin-bottom: 30px;
  }
  
  .question .answer {
    width: 250px;
    padding: 5px;
  }
  
  .button__section {
    margin-top: 55px;
    font: normal normal normal 1rem "Microsoft JhengHei";
  }
  
  .submit__btn {
    background: #fad312;
    border-radius: 3px;
    outline: none;
    border: none;
    padding: 1rem 2rem;
    margin-bottom: 20px;
    cursor: pointer;
  }
  
  .warning {
    font-size: 14px;
  }
  
  .copy__right {
    height: 30px;
    background: #000;
    color: #999;
    text-align: center;
    padding: 15px 0;
    line-height: 30px;
    font-family: "Microsoft JhengHei";
  }
  
  .warning__message {
    color: #e74149;
    font-size: 14px;
    position: absolute;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  
  &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;link rel="stylesheet" href="reset.css"&gt;
    &lt;link rel="stylesheet" href="style.css"&gt;
    &lt;title&gt;Lazy-Form&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
    &lt;div class="wrapper"&gt;
      &lt;form class="apply__form"&gt;
        &lt;h1 class="form__title"&gt;Form&lt;/h1&gt;
        &lt;div class="event__info"&gt;
          &lt;p class="event__time"&gt;Event date&lt;/p&gt;
          &lt;p class="event__place"&gt;Event Location&lt;/p&gt;
        &lt;/div&gt;
        &lt;h3 class="notice"&gt;Must&lt;/h3&gt;
        &lt;div class="questions"&gt;
          &lt;div class="question required"&gt;
            &lt;p class="question__title"&gt;Nickname&lt;/p&gt;
            &lt;input type="text" placeholder="Answer" class="answer"&gt;
          &lt;/div&gt;
          &lt;div class="question required"&gt;
            &lt;p class="question__title"&gt;email&lt;/p&gt;
            &lt;input type="text" placeholder="Answer" class="answer"&gt;
          &lt;/div&gt;
          &lt;div class="question required"&gt;
            &lt;p class="question__title"&gt;phone&lt;/p&gt;
            &lt;input type="text" placeholder="Answer" class="answer"&gt;
          &lt;/div&gt;
          &lt;div class="question required"&gt;
            &lt;p class="question__title"&gt;type&lt;/p&gt;
            &lt;div class="type1"&gt;
              &lt;input type="radio" name="answer" id="bed"&gt;
              &lt;label for="bed"&gt;dream&lt;/label&gt;
            &lt;/div&gt;
            &lt;div class="type2"&gt;
              &lt;input type="radio" name="answer" id="phone"&gt;
              &lt;label for="phone"&gt;walk around&lt;/label&gt;
            &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="question required"&gt;
            &lt;p class="question__title"&gt;How do you know?&lt;/p&gt;
            &lt;input type="text" placeholder="Answer" class="answer"&gt;
          &lt;/div&gt;
          &lt;div class="question"&gt;
            &lt;p class="question__title"&gt;Others&lt;/p&gt;
            &lt;input type="text" placeholder="Answer" class="answer"&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="button__section"&gt;
          &lt;button class="submit__btn"&gt;Submit&lt;/button&gt;
        &lt;/div&gt;
      &lt;/form&gt;
    &lt;/div&gt;
    &lt;script src="app.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>