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

173
Visualizações
How to prevent an eventListner for the first time on pageLoad In JS

i have used preventDefault() but thats not working here is My Code

btnBookNameSearch.addEventListener("click", (e) => {
    e.preventDefault();
    const searchValue = btnBookNameSearch.value.toLowerCase().trim();
 
    allPgBooks.forEach((product) => {
        if (product.name == searchValue) {
            allPgBooksE2.innerHTML = '';
            if (allPgBooksE2) { 

there is a mistake i am doing in the first two line of code above but idk how to fix ....the Error i am getting is ....Cannot read properties of null (reading 'addEventListener') ...

enter image description here

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

0

Your btnBookNamsSearch is null. Probably you trying to find the button by wrong id or selector.

about 4 years ago · Juan Pablo Isaza Relatório

0

It just means that the variable btnBookNameSearch is empty. It couldn't find the element that supposed to be inside that variable. It could be typo in the way you retrieved the element, e.g. document.querySelector('.element-class').

about 4 years ago · Juan Pablo Isaza Relatório

0

use DOMContentLoaded event. Add your code inside the callback function

document.addEventListener("DOMContentLoaded", function(event) {
    // Your code to run, DOM is loaded and ready
   btnBookNameSearch.addEventListener("click", (e) => {
   e.preventDefault();
   const searchValue = btnBookNameSearch.value.toLowerCase().trim();
   .....
   .....
});
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