Execute a function only if the DOM element exists.
npm install if-element-exists
import ifElementExists from 'if-element-exists';
ifElementExists('.foo', x => x.classList.add('bar'));
Calls func
if and only if the element is not null. Returns the result of func
.
Type: HTMLElement | string
DOM element, or query selector to get an element
Type: function
Receives the element as the first argument, if the element is not null. Return value of the function will be returned from ifElementExists
.