[Solved] Why do JavaScript functions work this way?


You’re in luck! The latter code will work in JavaScript as well.

Below, I’ve outlined three different ways you can achieve this using a more familiar syntax.

// The function to call
function MyFunc(){
  alert('Working!');
}

// Option 1 (Link 1)
document.getElementById("one").addEventListener("click", MyFunc);

// Option 2 (Link 2)
document.getElementById("two").onclick = MyFunc;
<a href="#" id="one">Link 1</a><br>
<a href="#" id="two">Link 2</a><br>
<a href="#" id="three" onclick="MyFunc()">Link 3</a>

8

solved Why do JavaScript functions work this way?