Currently my constructor has been defined like this and working :
function Carousel(parent, currentItem, count) {
var element = parent;
var count = count;
var arrPrev = document.createTextNode('‹');
var arrNext = document.createTextNode('›');
var cPrevClass = 'cPrevClass';
var cNextClass = 'cNextClass';
var crslClass = 'js-Carousel';
var current = currentItem;
showArrows();
/**
* Create the navigation arrows (prev/next) and attach to carousel.
*/
function showArrows() {
// stuff here
// Event listener (important for my question)
buttonPrev.addEventListener('click', showPrev);
buttonNext.addEventListener('click', showNext);
}
}
Because at the time I somehow forgot that I'm supposed to define my class constructor using the this keyword (I guess), I now have rewritten it like this :
function Carousel(parent, currentItem, count) {
this.element = parent;
this.count = count;
this.current = currentItem;
this.showArrows = showArrows;
this.showArrows();
}
/**
* Create the navigation arrows (prev/next) and attach to carousel.
*/
function showArrows() {
// stuff here...
var modal = document.getElementById("netreviews_media_modal");
// then adding event listener
buttonPrev.addEventListener('click', showPrev);
buttonNext.addEventListener('click', showNext);
modal.appendChild(buttonPrev);
modal.appendChild(buttonNext);
}
/**
* Go back
*/
function showPrev() {
if (Carousel.current == 0) {
Carousel.current = Carousel.count - 1;
}
else {
Carousel.current = Carousel.current - 1;
}
updateModal(element.querySelectorAll('.' + crslClass + ' > ul li > a')[Carousel.current]);
}
Now when the updateModal() function is running (see at the end of file), I have that error that tells me "Cannot read property 'querySelectorAll' of undefined"
Of course, element isn't defined anymore and just says 'undefined'.
I don't know how to make the element point to my class property. Also, Carousel.current isn't working either. And I dont know if Im doing good refactoring that. I haven't put all code (//stuff here) but I think its enough...
(FYI I do instantiate my Carousel function later in the code)
Any help appreciated !