Search code examples
javascripthtmlformspopup

Unintentional popup FORM closing when clicking on a BUTTON or on a INPUT ( with JS code)


I'm finding a strange behaviour in a popup FORM when I click on a BUTTON (that operates on some object using a JS code), and on a INPUT (used for submit): in both cases, the form closes, and it is an unexpected action. Probably is due to something very easy and common that I'm not fixing, but i can't find it.

This is the HTML interested part:

        <form name="contactform" id="contactform" class="contact-form">
          <div class="contactform-container">
            <a href="" class="closeButton"></a>
            <div class="common">
              <label for="name">Nome</label>
              <input type="text" id="name" name="name" />
            </div>
            <div class="common">
              <label for="name">Cognome</label>
              <input type="text" id="familyName" name="familyName" />
            </div>
            <div class="common">
              <label for="email">e-mail</label>
              <input type="text" id="email" name="email" />
            </div>
            <div class="message">
              <label for="message">Annotazioni</label>
              <textarea name="message" id="message" class="message"></textarea>
            </div>
            <div class="passRow">
              <fieldset class="validatePass">
                <div class="formGroup">
                  <label class="formLabel"for="password">Password
                    <span class="passErr"></span>
                  </label>
                  <div class="passWrapper">
                    <input type="password"
                    id="password"
                    class="form-control input-md"
                    name="password"
                    placeholder="Enter your password">
                    <span class="showPass">
                      <i class="fas fa-eye-slash"></i>
                    </span>
                  </div>
                  <p class="progress">Livello di sicurezza</p>
                  <div id="progressBar">
                    <div></div>
                  </div>
                  <ul id="progressList">
                    <li>Un carattere minuscolo e uno maiuscolo</li>
                    <li>Un numero</li>
                    <li>Un carattere speciale tra "!,%,&amp;,@,#,$,^,*,?,_,-"</li>
                    <li>Lunghezza minima: 8 caratteri</li>
                  </ul>
                </div>
              </fieldset>
            </div>
            <div class="securityCaptcha">
              <p>Inserire il codice nei riquadri sottostanti</p>
              <div class="first row">
                <div class="refCheck">
                  <canvas class="valiCaptcha"></canvas>
                </div>
                <div class="refCheck">
                  <canvas class="valiCaptcha"></canvas>
                </div>
                <div class="refCheck">
                  <canvas class="valiCaptcha"></canvas>
                </div>
                <div class="refCheck last">
                  <canvas class="valiCaptcha"></canvas>
                  <button class="reloadButton">
                    <i class="fas fa-redo"></i>
                  </button>
                </div>
              </div>
              <div class="second row">
                <div class="refCheck">
                  <input type="text" name="" maxlength="1">
                </div>
                <div class="refCheck">
                  <input type="text" name="" maxlength="1">
                </div>
                <div class="refCheck">
                  <input type="text" name="" maxlength="1">
                </div>
                <div class="refCheck">
                  <input type="text" name="" maxlength="1">
                </div>
              </div>
            </div>
            <div class="contactArea">
              <p>Compilare tutti i dati per la prenotazione.</p>
              <input type="submit" name="submit" id="submit" value="send">
            </div>
          </div>
        </form>
    </div>

The BUTTON that create problem is the one with class reloadButton.

The INPUT is the one with id submit.

I think css aren't necessary.

Regarding the JS part:

let formEls = formPopup.querySelectorAll('.common, .message, .note');
let charCode = [];
const refreshButton = document.querySelectorAll('.reloadButton')[0];
const passInput = document.getElementById('password');

window.onload = function () {
  document.querySelector('#reserveBtn').addEventListener('click', function () {
    formPopup.classList.add('active');
  });

  getCode();
  formPopup.querySelector('.closeButton').addEventListener('click', function () {
    cleanForm();
    formPopup.classList.remove('active');
  });

  formPopup.addEventListener('click', function (ev) {
      if (ev.target.id == 'contactform-bg') {
        cleanForm();
        formPopup.classList.remove('active');
      }
    });
  refreshButton.addEventListener('click', function (ev) {
    charCode = [];
    getCode();
  });

  passInput.addEventListener('keyup', function () {
    passVal = passInput.value;
    checkPass(passVal);
  });

};

let cleanForm = function () {
  formEls.forEach((item, i) => {
    item.classList.remove('typing');
  });
  // console.log(window['contactform-bg'].innerHTML);
  // console.log(document.getElementById('contactform').innerHTML);
  // console.log(document.contactform.innerHTML);
  document.contactform.name.value = '';
  document.contactform.familyName.value = '';
  document.contactform.email.value = '';
  document.contactform.message.value = '';
  passInput.value = '';
};

function getCode() {
  let sChars = 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9,!,@,#,$,%,^,&,*,(,)';
  let arrayChars = sChars.split(',');
  for (var i = 0; i <= 3; i++) {
    // trova un indice casuale tra 0 e la lunghezza dell'array
    RefIndex = Math.floor(Math.random() * arrayChars.length);

    // assegna il carattere estratto dall'array (strana indicazione del font come giapponese(??)
    let char = arrayChars[RefIndex];
    charCode[i] = char.toLowerCase;
    createImgCaptcha(char, i);
  }
}

I avoid to add the createImgCaptcha function because it just create CANVAS and doesn't have any impact on the matter.

Is there anyone able to explain to me why the FORM closes? I tried following the steps in JS but found no errors.

Thanks in advance.


Solution

  • Ok, I found the problems and fixed them. There were little mistakes one different from another. So, I list them:

    1. The "FORM close object": there was a missing '#' characer in the ref attribute, so I changed the HTML from <a href="" class="closeButton"></a> to <a href="#" class="closeButton" type="text/html"></a>, adding also the type attribute for greater completeness.
    2. The "class reloadButton": I finally read that a <button>Click to do something</button> is a default submit button. I didn't know... so, I just added the right type attribute to solve, in this way: <button class="reloadButton" type="button">.
    3. The "submit button": in this case, I changed the prevent JS command: function stopEvent(event) { event.preventDefault();}.

    Now, everything works correctly. Sorry if I bored you with these silly things; either way, it's always best to learn from mistakes.