Why func start when page is loaded

Need to launch function after click event. Help, please. Why it launchs auto without click on calculator Id

function check(){
    let youname = document.getElementById("youname").innerHTML
    alert(youname)
}

document.getElementById("calculator").onclick = check()

You are running the check function. The parentheses () will invoke the function.

The handler just takes the function identifier (name) as the callback, the handler will run the callback function for you when the event it is listening for fires.

document.getElementById("calculator").onclick = check;

1 Like