How to make an anchor tag to do nothing?

331 Views

How to make an anchor tag to do nothing?

HTML | Make an anchor tag to do nothing | href="javascript:void(0);"

To prevent reloading page when a link clicked is something like preventing the default behavior of an element.
It can be achieved by providing "javascript:void(0)" in "href" attribute.

<a href="javascript:void(0)">Click Here</a>

The "javascript:void(0)" make anchor tag "href" value undefined and make its behavior as a plain text.

To know more MSD Webdock

 


 

href="#"

The anchor tag click can also be prevented by simply using "return false" in "onclick" event, like so:

<a href="#" onclick="return false;">Click Here</a>

 


 

See the Example

In the example, There are four anchor tag with each having different property and value. jump page to the top

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
	<!-- Refress the page -->
    <a href="">Click Here</a>
	<br>
	
	<!-- Jump page to the top  -->
	<!-- Add # at the end of the url -->
	<a href="#">Click Here</a>
	<br>
	
	<!-- Do nothing -->
	<a href="#" onclick="return false;">Click Here</a>
	<br>
	
	<!-- Do nothing -->
	<a href="javascript:void(0)">Click Here</a>
</body>
</html>