Open In App

jQuery event.isDefaultPrevented() Method

Last Updated : 11 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

jQuery isDefaultPrevented() Method is an inbuilt method that checks whether the preventDefault() method was called for the event. This method returns a boolean value. It returns True if preventDefault() is called on the event, False otherwise. 

Syntax:

event.isDefaultPrevented()

Parameters:

It accepts a single parameter event that comes from the event binding function. 

Example 1: This example checks isPreventDefault() Method called on the event or not. 

HTML
<!doctype html>
<html>

<head>
    <title>
        isPreventDefault() Method
    </title>

    <script src=
"https://code.jquery.com/jquery-3.3.1.min.js">
    </script>
</head>

<body>
    <a href="https://www.geeksforgeeks.org">
        Go to Homepage
    </a>

    <div id="initial"></div>
    <div id="prevented"></div>
    <div id="response"></div>

    <!-- Script to check preventDefault() Method called or not -->
    <script>
        $("a").click(function (event) {

            $("#initial").html("Before: isDefaultPrevented? <strong>"
                + event.isDefaultPrevented() + "</strong>");

            event.preventDefault();

            $("#prevented").html("preventDefault() is called now.");

            $("#response").html("So, you are not going anywhere."
                + " isDefaultPrevented? <strong>"
                + event.isDefaultPrevented() + "</strong>");
        });
    </script>
</body>

</html>

Output: 

Note: The bold text (true/false) is the value of isDefaultPrevented() method. 

Example 2: This example check isDefaultPrevented() method prevent default action or not. 

HTML
<!doctype html>
<html>

<head>
    <title>
        isPreventDefault() Method
    </title>

    <script src="https://code.jquery.com/jquery-3.3.1.min.js">
    </script>
</head>

<body>
    <a href="https://www.geeksforgeeks.org">
        Go to Homepage
    </a>

    <div id="initial"></div>
    <div id="prevented"></div>
    <div id="response"></div>

    <!-- Script to check preventDefault() Method called or not -->
    <script>
        $("a").click(function (event) {

            $("#initial").html("Before: isDefaultPrevented? <strong>"
                + event.isDefaultPrevented() + "</strong>");

            event.preventDefault();

            $("#prevented").html("preventDefault() is called now.");

            $("#response").html("So, you are not going anywhere."
                + " isDefaultPrevented? <strong>"
                + event.isDefaultPrevented() + "</strong>");
        });
    </script>
</body>

</html>
<!doctype html>
<html>

<head>
    <title>
        isPreventDefault() Method
    </title>
    <script src=
"https://code.jquery.com/jquery-3.3.1.min.js">
    </script>
</head>

<body>

    <form action="action.php">
        Input:<br>
        <input type="text" name="input_1">
        <button type="submit">Submit</button>
    </form>

    <!-- Script to describe isDefaultPrevented() Method -->
    <script>
        $("button").click(function (event) {
            if (event.isDefaultPrevented())
                alert('Default action was prevented');
            else
                alert('Click Ok');

            event.preventDefault();

            if (event.isDefaultPrevented())
                alert('Default action was prevented');
            else
                alert('Click Ok');
        });
    </script>
</body>

</html>

Output: 


Next Article

Similar Reads