Skip to content

HTML a href call JavaScript function | Example code

  • by

You can use the href or on-click method to call a JavaScript function from HTML elements.

A href call function : href

<a href="javascript:yourFunction();">

HTML a href call JavaScript function example

The simple addition function will call on clicking the link, where the function will call from the href attribute.

In the example we are doing call JavaScript function from href with parameters, you can do it without parameters also.

<!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:

HTML a href call JavaScript function

Do comment if you have any doubts and suggestions on this HTML topic.

Note: The All HTML Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *