Skip to content

Execute JavaScript on link click | Example code

  • by

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.

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:

Execute JavaScript on link click

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading