A href JavaScript function call | Example code

Simple use javascript:void(0) as value of href and onclick method function name to call JavaScript function. Or another way is use javascript:method_name as 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 a 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


Leave a Reply

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