Javascript
JS intro
JS basics
JS variables
JS functions
JS popup boxes
JS conditional logic
JS loops
JS arrays
JS objects
JS strings
JS events
JS errors
JS DOM
JS elements
JS new windows
JS date and time
JS cookies
JS print
JS redirect
JS void 0
JS Summary

Programming

Programming intro
Java

Markup

First webpage guide
HTML
XHTML

Style & Layout

CSS

Browser scripting

VBScript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

Javascript void(0)

Usually, when the user clicks a link on a page, a new page loads or the same page refreshes but this is not always the desired course of action. For example, when the user clicks a link on a page, you might want to update the value of a field in a form or the value of a variable. To prevent a page from reloading when a link is clicked, the void(0) function is used.

Example:

<a href="" onclick="alert('You have clicked the button')>Click here!</a>

Output:

Click on the above link. Even though the code within it specifies to display a message in an alert box (and this action does take place), the page will reload automatically.

We can fix this with void(0):

Example:

<a href="javascript:void(0);" onclick="alert('You have clicked the button')>Click here!</a>

Output:

In the above example, void(0) prevents the page from reloading and a message is displayed in an alert box as intended.

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments