I think the easiest approach would be to just check that el
is not null before adding an event listener:
var el = document.getElementById('overlayBtn'); if(el){ el.addEventListener('click', swapper, false); }
Related Posts:
- addEventListener vs onclick
- Define a global variable in a JavaScript function
- how to fix : ” TypeError: Cannot read property ‘addEventListener’ of null”…?//
- How to create a jQuery function (a new jQuery method or plugin)?
- getElementById returns null?
- What is the difference between call and apply?
- document.getElementByID is not a function
- How would I call a javascript function in html?
- JavaScript getElementByID() not working
- Pass a JavaScript function as parameter
- includes() not working in all browsers
- Javascript removeEventListener not working
- How to remove all listeners in an element?
- Uncaught TypeError: .indexOf is not a function
- Anagrams finder in javascript
- document.getElementById not working / Display
- Can you write nested functions in JavaScript?
- Simple average function in Javascript
- How to add an object to an array
- What is define([ , function ]) in JavaScript?
- Stopping a JavaScript function when a certain condition is met
- moving an element in JS
- Simplest/cleanest way to implement a singleton in JavaScript
- Return array from function
- Javascript call() & apply() vs bind()?
- JavaScript button onclick not working
- JavaScript button onclick not working
- anchor jumping by using javascript
- Javascript- Multiplying 2 numbers and return number
- $http.get(…).success is not a function
- How do I check if an array includes a value in JavaScript?
- JavaScript document.getElementById().value logs empty value
- Client on Node.js: Uncaught ReferenceError: require is not defined
- How can a Discord bot create a hyperlink in a Discord message in an embed or in general?
- Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai
- Error: Can’t set headers after they are sent to the client
- Express error – TypeError: Router.use() requires middleware function but got a Object
- How can I check for “undefined” in JavaScript? [duplicate]
- How can I reverse an array in JavaScript without using libraries?
- Does JavaScript pass by reference? [duplicate]
- How to resolve the C:\fakepath?
- React this.setState is not a function
- AngularJS- Login and Authentication in each route and controller
- Check if a variable is a string in JavaScript
- MRAID file reference
- How to format numbers?
- Does parseDouble exist in JavaScript?
- What does !1 and !0 mean in Javascript?
- How to use php code in javascript
- How to read xml file contents in jQuery and display in html elements?
- node.js TypeError: path must be absolute or specify root to res.sendFile [failed to parse JSON]
- Delete first character of a string in Javascript
- How do I get the current time only in JavaScript
- Shortest way to print current year in a website
- push object into array
- module.exports “Module is not defined”
- Why doesn’t JavaScript support multithreading?
- plain count up timer in javascript
- Uncaught TypeError: Cannot read property ‘name’ of undefined
- How to calculate date difference in JavaScript?
- React’s setState method with prevState argument
- Caesar Cipher in Javascript
- Can’t perform a React state update on an unmounted component
- How to simulate a click with JavaScript?
- This.props.dispatch not a function – React-Redux
- this vs $(this)
- How to scroll an HTML page to a given anchor
- “google is not defined” when using Google Maps V3 in Firefox remotely
- how to call a onclick function in tag?
- How to sort an array of integers correctly
- Failed to execute ‘postMessage’ on ‘DOMWindow’: https://www.youtube.com !== http://localhost:9000
- How can I check whether a radio button is selected with JavaScript?
- JavaScript Array Push key value
- stopPropagation vs. stopImmediatePropagation
- typescript – cloning object
- Ajax success function
- Register and enqueue conditional (browser-specific) javascript files?
- How to replace regular jquery calls with CDN calls from Google?
- Editing Source Code in WordPress
- Why is this gutenberg custom format button only available from the toolbar as a dropdown?
- VB.net and WordPress
- Gutenberg passing block attributes to component in ES6/ESNext
- WP REST API Post Status Using JavaScript
- Script Localization doesn’t work
- How to conditionally show/hide controls & sections in WordPress customizer?
- Uncaught TypeError [closed]
- How to move core js files into the footer
- Building a Featured Gallery component for Gutenberg
- Retrieving data about comments and likes
- Including Styles and JS files to work ON my plugin interface
- Linking wp_enqueue can’t find the javascript file (adds “?ver=x.x.x” to the src)
- My code will not execute in wordpress, even though I’ve been told the code is fine
- JSX SVG Icon with WordPress
- Javascript working on jsfiddle but not wordpress site?
- Drodown list return issue
- Spans in gutenberg
- Put dynamic Javascript in header after doing operations
- Adding javascript blocks to a single file and adding it to the header
- How to integrate a different (JavaScript) editor in WordPress?
- How can I look at/edit what’s being sent to post.php when I hit the publish/update button?