Wednesday, July 3, 2024
HomeLanguagesJavascriptHow to perform jQuery Callback after submitting the form ?

How to perform jQuery Callback after submitting the form ?

The after-form-submit is the set of code that is executed after a submit button is fired. Usually, it is used to blank out the form, once the submit is given. For Example, When the login credentials are wrong, the input becomes blank in the form.

Approach:

  1. First, the elements in a form must be ensured if it is loaded.
  2. Then, the submit method is added a callback function.
  3. Once the submit button is clicked, it should prevent successive callbacks.
  4. Followed by that, a timeout is given because of a need for delay. If there is no delay, the actions that should happen after would happen at the beginning itself. Like before validating the password, the input form shouldn’t blank out.
  5. Then the actions that need to be performed are given at the end.

How to implement in Javascript ?

  1. A callback to the jQuery method can be done using the submit button.
  2. Once the submit button of the form is clicked, the callback function is fired. But, before that, the DOM (document object manipulation) must be loaded. Once the DOM is loaded, it then calls any of these methods. ( window.on(‘load’) or document.ready() or window.ready() can be used).
  3. The preventDefault() method prevents the method from firing multiple times.
  4. We use setTimeout method to perform after submit with few milliseconds.

Note: The window.ready() method is called when all the resources are loaded and document.ready() method is called when DOM is loaded.

Example:




<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to perform jQuery Callback
        after submitting the form ?
    </title>
  
    <script src=
    </script>
  
    <script>
        $(window).ready(function () {
  
            $("form").submit(function (e) {
                e.preventDefault();
                var name = $("#name").val();
                setTimeout(function () {
                    console.log("After timeout");
                    $('#name').val('');
                }, 100);
                alert(name + " has submitted");
            })
  
        })
    </script>
</head>
  
<body>
    <form action="">
        <input type="text" id="name">
        <input type="submit">
    </form>
</body>
  
</html>


Output:

This form submit method triggers the callback method, which at first prevents further callbacks. Then after a time interval of 100 milliseconds, the alert method is called with the text input value.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments