Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
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 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!