Loot Cannot Read Property 'getelementsbyclassname' of Null

The method addEventListener() is used in conjunction with EventTarget, which is a Document Object Model (DOM) implementation that tin receive events like click, mouseover, submit, keydown, elevate, and more. Events tin be thought of as the interaction a user has with the spider web page or document.

Unlike the value of undefined, the aught value can be a value assigned on purpose by developers  into their programs. It represents the absence of any value or object. It is a primitive and falsy value.

In this article, nosotros'll dive into why developers may receive this error when coding.

Why the Mistake of Nil is Triggered

An event listener was added to the image of the ship on the website below. We should be seeing a blueish dotted border around the image upon clicking it, but instead nosotros receive the 'Cannot read property 'addEventListener' of null' error in the console.

Screenshot 397

Misspelling the name of the course when invoking the querSelector() method could be the reason why. Remember that the value of null means the absence of a value or object. By calling on a value that doesn't exist the error of zero volition announced.

Note that the querySelector method is just one of many methods we can use when choosing to dispense a CSS selector in the DOM. The difference between it and the Get Methods like .getElementsByClassName is that when calling a course or an id using querySelector or querySelectorAll, nosotros need to retrieve to use the dot and hash symbol.

An instance to be used inside the invocation is ('.insertClassName') when calling the name of the class or ('#insertIdName') when calling the name of the id. Forgetting to do so will upshot in the same error.

The image of the example website to a higher place is a representation of the post-obit markup and JavaScript lawmaking.

81% of participants stated they felt more than confident nigh their tech task prospects afterward attention a bootcamp. Go matched to a bootcamp today.

The average bootcamp grad spent less than 6 months in career transition, from starting a bootcamp to finding their get-go job.

HTML

            <section course="content-destination">       <h2>Choice Your Destination</h2>       <p>Expedition excursion pattern darn excursion fun, clean unproblematic organized WordPress Travel colorful webdesign. Traveler blogger website blueprint expedition make clean excursion traveling.</p>       <img src="img/destination.jpg" alt="Second slide">     </section>

JavaScript

            const thirdBorder = document.querySelector('.content-dest');  thirdBorder.addEventListener('click', (event) => {      result.target.style.border = '1rem dotted blue'      issue.stopPropagation();     });          

Notice when nosotros invoke the querySelector method, we are calling on the grade '.content-dest' when '.content-dest' does not exist in the HTML. If we would take forgotten to use the dot in the grade name, we would take received the same error as well.

Debugging

The value we should be selecting in our JavaScript is '.content-destination'. Upon correcting this, the error in the console disappears, and we see the blueish dotted border created in our event listener.

            const thirdBorder = document.querySelector('.content-destination');  thirdBorder.addEventListener('click', (outcome) => {      event.target.style.border = '1rem dotted bluish'      event.stopPropagation();     });          
Screenshot 400

Conclusion

The addEventListener method is used as a way for users to interact with the web page or certificate. Nil is the absence of a value or object. When we phone call on a selector that does not be, or forget to include the dot in class name or hash symbol in id when invoking the querySelector methods, we can trigger the aforementioned error of null.

cooneybeity1995.blogspot.com

Source: https://careerkarma.com/blog/cannot-read-property-addeventlistener-of-null/

0 Response to "Loot Cannot Read Property 'getelementsbyclassname' of Null"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel