How To Reset Modal Form In jQuery

Websolutionstuff | Jan-04-2023 | Categories : Laravel PHP jQuery Bootstrap

Have you ever seen those pop-up boxes on websites? They're called modal forms, and they make it easier to do things on the website without needing to reload the whole page.

Now, imagine if you could learn how to make those boxes start over without having to click anything - that's a cool skill! This article will show you how to do just that, using a tool called jQuery. It's like a set of instructions that help you build cool stuff for websites.

No matter if you're good at making websites or just starting, we'll guide you step by step. You'll learn how to make those pop-up boxes reset and start fresh. We'll explain things clearly and give examples, so you'll feel confident to try it out in your own projects.
 

Add HTML

In this step, we will add a bootstrap modal with a form. So, add the following code to the HTML file.

<div class="container">
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
  <div class="modal fade" id="myModalExample" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
                  How To Reset Modal Form In jQuery - Websolutionstuff
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        <div class="modal-body">
  <form action="">
    <div class="form-group">
      <label for="email">Email:</label>
      <input type="text" class="form-control" id="email" placeholder="Enter email" name="email">
    </div>
  </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>      
    </div>
  </div>  
</div>

 

Add jQuery

Add the following jquery to reset the form. So, add the following code to the script tag.

$("#myModalExample").modal("show");
$('#myModalExample').on('hidden.bs.modal', function () {
    $('#myModal form')[0].reset();
});

Also, you can trigger the reset() function like the below code.

$('#MyModalExample').on('hidden.bs.modal', function () {
    $(this).find('form').trigger('reset');
})

Output:

how_to_reset_modal_form_using_jquery

 

Conclusion

Learning how to reset popup forms with jQuery lets you make the experience better for users. By doing a few simple things, you can clear the form fields and make it really easy for people 4. using your website.

If you follow these tips, you'll know how to reset forms in a way that works well. This means that when people fill out forms on your website, it will be super smooth and not confusing. So go ahead and try these ideas; you'll see that more people will like using your website!
 


You might also like:

Recommended Post
Featured Post
How To Bind Data In React JS
How To Bind Data In React JS

In this article, we will see how to bind data in React JS. Also, we will see how to bind the variable value in the...

Read More

Aug-19-2022

Laravel 9 React JS CRUD Operation Example
Laravel 9 React JS CRUD Operat...

This article will show the laravel 9 react js crud operation example. Here, we will learn how to create crud operation i...

Read More

Dec-08-2022

How to Get Soft Deleted Records in Laravel 10
How to Get Soft Deleted Record...

Hey there! Ever wondered how to recover deleted data in Laravel 10 without much hassle? Well, you're in luck! I'...

Read More

Nov-27-2023

Drag and Drop File Upload Using Dropzone js in Laravel 9
Drag and Drop File Upload Usin...

In this article, we will see drag and drop file upload using dropzone js in laravel 9. Dropzone JS is an open-source lib...

Read More

Mar-19-2022