Using JavaScript void(0) with onClick prevents the default action of an HTML element, such as a link or a button, when it is clicked. To prevent the page from reloading upon a single click, we can use JavaScript:void(0); within the anchor link.
JavaScript void(0) onClick example
Simple HTML example code won’t perform any default action and execute the JavaScript code alert('Hello World!')
on click.
<!DOCTYPE html>
<html>
<body>
<button onClick="javascript:void(0); alert('JavaScript void(0) onClick')">Click me</button>
</body>
</html>
Output:
Comment if you have any doubts or suggestions on this JS void topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version