I'm calling a function when I press a button. When I use the btn.onclick = function()
the button is pressed and the function is completed. If I change the HTML to include onclick=clicked()
and the javascript to function clicked()
, it does not run.
I've tried both these methods because I want to be able to pass a parameter through the function (as I have lots of buttons and I want to use as little code as possible).
window.onload = function () {
var modal = document.getElementById('myModal');
var btn = document.getElementById("Q11");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function () {
modal.style.display = "block";
}
span.onclick = function (){
modal.style.display ="none";
}
window.onclick = function(event) {
if (event.target==modal) {
modal.style.display="none"
}
}
}
How do I get it where I can pass a parameter through the btn.onclick
function so that any button can call that function but pass a different parameter through it? Is there an equivalent for onclick="function(a)"
but for event handlers/listeners?
Any help is appreciated.