Javascript Event Execution Error

Asked

Viewed 54 times

0

I have a problem in Javascript that indicates that the line is in error, I compared with other codes but I see nothing different from other codes...

HTML

<script src="../js/contact.js" defer></script>


  <section id="support" class="row wide-container valign-wrapper">
    <div class="col s12 m6 l6 description">
      <h3><b>Precisa de ajuda?</b><br />Envie uma mensagem!</h3>
    </div>
    <div class="col s12 m6 l6 form-container">
      <form wrapper="materialize_vertical_form" action="#!" method="post">
        <div class="input-field ">
          <input type="text" name="name" id="name">
          <label class="required">* Nome</label>
          <div class="help-block grey-text"></div>
        </div>
        <div class="input-field ">
          <input type="email" name="mail" id="mail">
          <label class="required">* E-mail</label>
          <div class="help-block grey-text"></div>
        </div>
        <div class="input-field ">
          <textarea name="message" id="message" class="materialize-textarea"></textarea>
          <label class="required">* Mensagem</label>
          <div class="help-block grey-text"></div>
        </div>
        <div class="input-field center-align">
          <button class="btn cta waves-effect waves-light btn-atendimento pulse-hover" type="submit" name="_sendMessage" id="_sendMessage">Enviar Mensagem</button>
        </div>
      </form>
    </div>
    <div class="darken-backdrop"></div>
  </section>

Contact js.

window.onload = () => {
    //Constantes que são pegas do Formulário
    const name = document.getElementById('name');
    const mail = document.getElementById('mail');
    const message = document.getElementById('message');
    const sendMessage = document.getElementById('sendMessage');
(7) sendMessage.addEventListener('submit', (name, mail, message) => {

        if(name.value == undefined || name.value == "" || name.value == null){
            alert('Vazio!');
        } else {

        }
    });
}

That would be because I still haven’t done what goes inside the Else?

Error

contact.js:7 Uncaught TypeError: Cannot read property 'addEventListener' of null
    at window.onload (contact.js:7)
  • Hello @Devprogramming, Welcome to Sopt, you need to add your code html - [mcve] . But apparently the error indicates that the code document.getElementById('sendMessage') not finding an element with id sendMessage in your html.

  • Thank you @Icaro and thank you for the welcome.

  • 2

    Thank you all, I was able to solve the error I was giving in this question. was the sendMessage that was with id="_sendMessage".

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.