HTML a href call JavaScript function | Example code

You can use 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

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

In the example we are doing call JavaScript function from href with parameters, you can do it without parameter 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.