Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to make an anchor tag refer to nothing?
To make an anchor tag refer to nothing, use javascript:void(0). The following link does nothing because the expression "0" has no effect in JavaScript. Here the expression "0" is evaluated, but it is not loaded back into the current document.
Using javascript:void(0)
The javascript:void(0) approach prevents the default link behavior and returns undefined, making the link inactive.
<!DOCTYPE html>
<html>
<head>
<title>Inactive Anchor Tag</title>
</head>
<body>
<p>Click the following, This won't react at all...</p>
<a href="javascript:void(0)">Click me!</a>
</body>
</html>
Alternative Methods
There are several ways to create non-functional anchor tags:
<!DOCTYPE html>
<html>
<head>
<title>Inactive Links Methods</title>
</head>
<body>
<!-- Method 1: javascript:void(0) -->
<a href="javascript:void(0)">Method 1: void(0)</a><br><br>
<!-- Method 2: Empty href -->
<a href="#">Method 2: Hash only</a><br><br>
<!-- Method 3: Prevent default with onclick -->
<a href="#" onclick="return false;">Method 3: Return false</a><br><br>
<!-- Method 4: Using button instead -->
<button type="button">Method 4: Button element</button>
</body>
</html>
Comparison of Methods
| Method | Behavior | Page Jump | Best Use |
|---|---|---|---|
javascript:void(0) |
No action | No | Placeholder links |
href="#" |
Jumps to top | Yes | Quick testing |
return false |
Prevents default | No | With JavaScript logic |
<button> |
No navigation | No | Interactive elements |
Why Use javascript:void(0)?
The void operator evaluates an expression and returns undefined. Using void(0) ensures the link performs no action and doesn't navigate anywhere, making it ideal for placeholder links during development.
Conclusion
Use javascript:void(0) for truly inactive anchor tags. For interactive elements that aren't links, consider using <button> instead for better semantic HTML.
