I know similar questions have been asked, but I still can't get this quite right. In this code, after 3 seconds, a message appears saying you can click the box. When you click it, an alert appears. That's what I want. Notice the function call has no parantheses. If you add the parentheses like this onclick=myfuction()
it doesn't wait for the click. It just displays the alert. What if I wanted to pass a value to that function, if I try to do onclick=myfuction("somedata")
It won't wait for the click. Is there a way to pass data to the function in this situation?
setTimeout(function(){
document.getElementById('okmessage').style.display='block',
document.getElementById('mydiv').onclick=myfunction,
document.getElementById('mydiv').style.cursor='pointer'},3000);
function myfunction() {alert("hello world")}
<div id="mydiv" style="width:300px;height:100px;background:red"></div>
<div id="okmessage" style="display:none">click the box now</div>