I have an event listener which includes an event handler with parameters ... and it works ... and, from what I have read, it is not supposed to. When I include the event handler in an anonymous function, like (I think that) I'm supposed to, it stops working.
I have searched this forum for similar issues and all of the questions are answered the same way (paraphrasing): "if you want parameters in your event handler, you have to put it in an anonymous function."
Apologies if this is a mundane question, I am new to this (and I did search for duplicate questions): But, why does this work and, more importantly, is there a better way to do this?
[Clarifying info: I have 5 buttons on a page, hence the loop. Each button controls a different area of the webpage (but with the same action, change css styling from "display:none" to "display:block") - which is why I need a one-to-one correspondence between an individual button and an individual "details" tag, hence the need for parameters in the event handler. Finally, the buttons toggle, hence the "if ... else".]
p.s I have a put a page online, temporarily, so you can see how it works (it is just a "notes to myself" page and is incomplete) : http://www.mylescallan.com/gameDesign/gameDesignIntroduction.html
var buttons = document.getElementsByClassName("expand"),
    details = document.getElementsByClassName("reveal"),
    i;
function makeClickHandler(details, i) {
    "use strict";
    function myHandler() {
    if (details[i].style.display === 'block') {
        details[i].style.display = 'none';
        buttons[i].innerHTML = "<em>Click Here:</em> To Expand The Source  Code For This Section";
        buttons[i].style.opacity = "1";
    } else {
        details[i].style.display = 'block';
        buttons[i].innerHTML = "<em>Click Here<em>: Don't Forget To Hide This Section, You've Read It";
        buttons[i].style.opacity = "0.5";
    }
};
    return myHandler;
}
for ( i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", makeClickHandler(details, i), false);
}
 
     
    