I'm trying to add a clicklist eventListener to my divs through their classes, which are: option-title . However as it is more of a title, I would like it to retrieve exactly the one I am clicking.
Follow the code:
var groupOptions = document.getElementsByClassName("option-title");
for(var i = 0; i < groupOptions.length; i++){
groupOptions[i].addEventListener("click", message(i));
}
function message(i){
if(i == 1){
alert("Clicado 1!");
}
else if(i == 2 ){
alert ("Clicado 2");
}else if(i == 3){
alert("Clicado 3");
}else if(i == 4){
alert("Clicado 4");
}
}
<div class="box-option option-title">
<span>Title 1</span>
</div>
<div class="box-option option-title">
<span>Title 2</span>
</div>
<div class="box-option option-title">
<span>Title 3</span>
</div>
<div class="box-option option-title">
<span>Title 4</span>
</div>
What I would like to see happen is that by clicking on, for example, the third div, it would give me its index (2). Another question is also if I can pass some parameter when I am adding the EventListener, because I tried to pass the index. Thankful.