I have this JS code in a widget which works fine on the page it the widget loads. But gives errors ("new.js?ver=0.0.1:92 Uncaught TypeError: Cannot read property 'querySelector' of null") on the page the widget it's not showing on here's jS code -
var
carousel = document.querySelector('.threedcarousel'),
figure = carousel.querySelector('figure'),
nav = carousel.querySelector('nav'),
numImages = figure.childElementCount,
theta = 2 * Math.PI / numImages,
currImage = 0
;
nav.addEventListener('click', onClick, true);
function onClick(e) {
e.stopPropagation();
var t = e.target;
if (t.tagName.toUpperCase() != 'BUTTON')
return;
if (t.classList.contains('next')) {
currImage++;
}
else {
currImage--;
}
figure.style.transform = `rotateY(${currImage * -theta}rad)`;
}
As I understand since the widget is not loading other pages hence the errors, But how I resolve the error ? tried but didn't work
if(typeof carousel == 'undefined') {
var carousel = document.querySelector('.threedcarousel')
}