There are many ways to Execute a JavaScript function in an HTML document. Using the onclick event attribute simple way to Execute JavaScript on link click.
Execute JavaScript on link click Example
HTML example code: use the onclick event, something like this:
<!DOCTYPE HTML>
<html>
<body>
<a href="javascript:add(1,2)" id="fo">JS Add Numbers</a>
<script>
function add(a,b){
alert(a+b);
}
</script>
</body>
</html>
Output:
Another way using the href
<a href="#" onclick="Hello(); return false;">LINK</a>
<script>
function Hello(){
console.log("Hello function")
}
</script>
Do comment if you have any doubts and suggestions on this JS link topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version