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);
}
More Related Contents:
- addEventListener Two Functions
- What is wrong with this function
- What does the exclamation mark do before the function?
- What is the use of the JavaScript ‘bind’ method?
- How can I call PHP functions by JavaScript?
- Are eval() and new Function() the same thing?
- Why can I use a function before it’s defined in JavaScript?
- Call Python function from JavaScript code
- Adding console.log to every function automatically
- Calling a JavaScript function named in a variable [duplicate]
- How can I create every combination possible for the contents of two arrays?
- What is “function*” in JavaScript?
- How to define custom sort function in javascript?
- JavaScript getElementByID() not working [duplicate]
- Javascript can’t find element by id? [duplicate]
- Javascript: Uncaught TypeError: Cannot call method ‘addEventListener’ of null
- JavaScript curry: what are the practical applications?
- Calling a javascript function recursively
- How to make chainable function in JavaScript?
- Access variable outside function scope [duplicate]
- Is there a JavaScript function that reduces a fraction
- Javascript multiple dynamic addEventListener created in for loop – passing parameters not working
- getElementById returning value only for first element [closed]
- What happens if I call a function with more arguments than it is defined to accept?
- The reason to use JS .call() method?
- Can you create functions with custom prototypes in JavaScript?
- Use JavaScript variable as function name?
- difference between parseInt() and parseFloat() [duplicate]
- Passing a function with arguments as an argument?
- javascript click event handler fires without clicking