I am confused as to why I am getting the console logging from formating(). When the code seems to be putting action listeners on the buttons within my HTML. What I would expect here is the code to wait until the user clicked to run formating(). I'm sorry if this is poorly worded I'm new to coding and trying to teach myself.
ps. I understand that there are easier ways to do this via Jquery but I am trying to teach myself the hard way first lol.
let i = 0;
while (i < data.length && i <= 8)
{
//some other code above
selectionA(i);
i++;
}
function selectionA(selected)
{
var classname = document.getElementsByClassname("classname");
classname[selected].addEventListener("click", formating(selected), false);
}
function formating(i)
{
console.log("you clicked on : + i = " class"
}
Result:
you clicked on: 0 class
you clicked on: 1 class
you clicked on: 2 class
you clicked on: 3 class
you clicked on: 4 class
you clicked on: 5 class
you clicked on: 6 class
you clicked on: 7 class
you clicked on: 8 class
without any class being clicked on.
Thanks for any help or suggestions anyone has!