Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

100
Visualizações
how to add addEventListener to generic js file loaded by files not containing object listened

I have a js file with multiple functions around a single domain. This file is used by multiple php files. In the js I have 2 addEventListener for 2 forms, but both forms are in different php files. Now if I am loading the file that does NOT have <form id="quote">, the js

  const formQuote = document.querySelector('#quote');
  formQuote.addEventListener('submit', (event) => {

wants to create an addEventListener on a NULL object. How do I prevent this (sorry, new to php, js, etc).

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can just check if the element is null or not using an if statement. Value null is a falsy value, which means in if statement it evaluates to false. You can read more about falsy values in JavaScript here: https://www.freecodecamp.org/news/falsy-values-in-javascript/

const formQuote = document.querySelector('#quote');

if (formQuote) {
  formQuote.addEventListener('submit', (event) => {
    console.log('test')
  })
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda