How is javascript(-s) to write?

as it is using javascript to rewrite?
$('.slide').click(function () {
 var index = $(this).index();
console.log(index)
});


When pressed .slide gives its index to the console.
March 23rd 20 at 18:54
4 answers
March 23rd 20 at 18:56
Solution
document.addEventListener('click', e => {
 const slide = e.target.closest('.slide');
 if (slide) {
 const index = [...slide.parentNode.children].indexOf(slide);
console.log(index);
}
});
March 23rd 20 at 18:58
Solution
const slides = document.querySelectorAll('.slide');

slides.forEach(slide => slide.addEventListener('click', function () {
 const index = [...this.parentNode.children].indexOf(this);

console.log(index);
}));


March 23rd 20 at 19:00
var slides = document.getElementsByClassName('slide');
for(let i = 0 ; i < slides.length ; i++) {
 slides[i].addEventListener('click', function() {
console.log(i);
});
}
March 23rd 20 at 19:02
document.querySelectorAll('.slide').forEach(
 (el, i) => el.addEventListener('click', () => console.log(1+i))
)


Find more questions by tags JavaScript