Open In App

When to use PreventDefault() vs Return false in JavaScript ?

In Javascript, there are two methods- preventDefault() and Return false, that are used to stop default browser behaviour but their functionalities and uses are little different. This article shows how these two are different.

JavaScript preventDefault() Method:



This method stops the event if it is stoppable, meaning that the default action that belongs to the event will not occur. It just prevents the default browser behaviour. Developers use preventDefault() in many cases For example,

Return false



Return false follow three steps

Developers use the return false in many different cases. For example,

Note: This behaviour differs from normal (non-jQuery) event handlers, in which, notably, return false does not stop the event from bubbling up. Returning false from a regular DOM event handler does absolutely nothing. 

Below are some examples to demonstrate the above: 

Example 1: Behaviour without PreventDefault( ) and Return false 




<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
 
    <h2>without PreventDefault() and return false</h2>
    <div onclick='GeekParent()'>
        <a href='#' onclick='GeekChild()'>
            Click here
        </a>
    </div>
    <br>
    <br>
    <script>
        function GeekChild() {
            alert('Link Clicked');
        }
         
        function GeekParent() {
            alert('div Clicked');
        }
    </script>
</body>

Output:

 

Explanation: The link performs its default action as no condition is mentioned to stop it. 

Example 2: with PreventDefault() 




<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h2> PreventDefault( ) </h2>
    <div onclick='GeekParent()'>
        <a href='https://ide.geeksforgeeks.org'
           onclick='GeekChild()'>
            Click here to visit GeeksforGeeks.com
          </a>
    </div>
    <br>
    <br>
    <script>
        function GeekChild() {
            event.preventDefault();
            event.currentTarget.innerHTML = 'Click event prevented'
            alert('Link Clicked');
        }
         
        function GeekParent() {
            alert('div Clicked');
        }
    </script>
</body>

Output: 

 

Explanation: In the output, the default function of the link i.e. connecting to another website has been stopped. Instead, an alert is shown using the preventDefault method.

Example 3: with Return false depending upon boolean (true or false) 




<script src=
</script>
<body style="text-align:center;">
    <h2>Form must be filled</h2>
    <h5>otherwise return false stop event</h5>
    <form name="myForm"
          onsubmit="return validateForm()"
          method="post">
        Name:
        <input type="text" name="fname">
        <input type="submit" value="Submit">
    </form>
    <script>
        function validateForm() {
            var x = document.forms["myForm"]["fname"].value;
            if (x == "") {
                alert("Name must be filled out");
                return false;
            }
        }
    </script>
</body>

Output:

 

Explanation: Since the button is pressed without filling the input field an alert appears because of the condition set in <script> tag which prevents default submit button action and returns false.


Article Tags :