Skip to content

A href JavaScript function call | Example code

Simple use javascript:void(0) as the value of href and onclick method function name to call JavaScript function. Or another way is to use javascript:method_name as the value of href.

href="javascript:Method_Name()"

A href JavaScript function call Example

HTML example code:

A href call function

<!DOCTYPE HTML>
<html>

<body>
	<a href="javascript:num(000);"> Call JS Function</a>

	<script>
		function num(value){
			alert("Number " + value);
		}
	</script>
</body>

</html>

Output:

A href JavaScript function call

If you only have as “click event handler”, use an instead. A link has a specific semantic meaning.

<button onclick="ShowOld(2367,146986,2)">
 Click
</button>

Another way

<a href="#" onclick="javascript:Show(123456)">

How to call JavaScript function instead of href in HTML?

Here is syntax should work call JavaScript function instead of href in HTML:-

<a href="javascript:void(0);" onclick="Show(123);">

or

<a href="javascript:Show(123);">

Do comment if you have any doubts and suggestions on this JS href examples code.

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

OS: Windows 10

Code: HTML 5 Version

Tags:

Leave a Reply

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