How to Use JavaScript: void(0) and onClick? Last Updated : 12 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Using javascript:void(0) in combination with the onClick attribute is a technique used in HTML to create links or clickable elements that perform actions without navigating away from the page. This is common when you want to attach a JavaScript function or event handler to a clickable element but prevent the default behavior of links (such as following an href).Explanation:javascript:void(0) is an expression that evaluates to undefined and effectively does nothing. It is used to prevent the default action of the link (<a> element) when clicked.onClick is an HTML attribute that allows you to attach a JavaScript event handler to an element, so a function or block of code is executed when the element is clicked.Example of Using javascript:void(0) with onClick HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Using javascript:void(0) with onClick</title> </head> <body> <!-- Anchor tag using javascript:void(0) with an onClick event --> <a href="javascript:void(0)" onclick="alert('Link clicked!')">Click me</a> <script> function handleClick() { alert('Button clicked!'); } </script> <!-- Button using onClick directly --> <button onclick="handleClick()">Click this button</button> </body> </html> Output:OutputClicking ButtonClicking LinkExplanation:<a href="javascript:void(0)">:href="javascript:void(0)" ensures that clicking the link does not trigger navigation or reload the page.onclick="alert('Link clicked!')" runs a JavaScript function or code when the link is clicked.onclick Attribute on Button:You can attach JavaScript functions directly to buttons or other elements using the onclick attribute, as demonstrated with the <button> element.When to Use javascript:void(0):Prevent Default Navigation: It’s often used to prevent <a> tags from following a URL when clicked, allowing you to handle the click event with JavaScript instead.Single Page Applications (SPAs): Frequently used in SPAs where page reloads are generally avoided.Alternative Approaches:Instead of using javascript:void(0), you can use # as the href attribute to create a "dummy" link. However, this may cause the page to scroll to the top, so you need to prevent the default behavior explicitly: HTML <a href="#" onclick="event.preventDefault(); alert('Link clicked!')">Click me</a> Output:Outputevent.preventDefault() prevents the default action (in this case, navigating to the top of the page).Summary:javascript:void(0) is used to prevent navigation or reloads for clickable elements like links.onclick attaches a JavaScript function or code that runs when the element is clicked.Use these approaches to control the behavior of links and buttons in your web applications Create Quiz Comment A anuragtriarna Follow 0 Improve A anuragtriarna Follow 0 Improve Article Tags : JavaScript Web Technologies Web QnA Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like