How to use addEventListener with function parameters in JavaScript?

To pass the arguments inside the function with addEventListener you can use, for example, .bind().

yourElement.addEventListener("click", yourFunction.bind(null, argument1, argument2, '...'));

Make sure to set the extra first argument to null, false, or this.


<button id="click">Click</button>
<div id="result"></div>

    document.getElementById('click').addEventListener("click", printFunction.bind(null, 'My Test'));

    function printFunction(myText) {
        document.getElementById('result').innerHTML = myText;