How to clear modal values after submitting

extension23

I am new to Meteor and Javascript. I have slowly figured out how to set up a modal form that will show up when I click a button, and can capture my form data and have the form close when I click submit. Where I am stuck is that whenever I click the button to add more data, the previous values are still in the modal form. I have tried most of the other answer and examples I have found, but I can't seem to figure out what I need to do to make it clear the values. Any suggestions would be appreciated. Here is what I have currently:

Modal Template

<template name="addButton">
  <div class="modal fade" id="addButton">
  <form>
    <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Add Button</h4>
      </div>
      <div class="modal-body">
        <form role="form">
            <div class="form-group">
                <label for="buttonOrder">Button Order</label>
                <input type="text" class="form-control" name="buttonOrder">
            </div>
            <div class="form-group">
                <label for="buttonName">Button Name</label>
                <input type="text" class="form-control" name="buttonName">
            </div>
            <div class="form-group">
                <label for="buttonDescription">Button Description</label>
                <input type="text" class="form-control" name="buttonDescription">
            </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
        <input type="submit" value="Submit" class="btn btn-primary"/>
      </div>
    </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </form>
  </div>
</template>

Modal JS Helper

Template.addButton.events({
    'submit form': function(e) {
        e.preventDefault();

        var button = {
            buttonOrder: $(e.target).find('[name=buttonOrder]').val(),
            buttonName: $(e.target).find('[name=buttonName]').val(),
            buttonDescription: $(e.target).find('[name=buttonDescription]').val()
        }
        button._id = Buttons.insert(button);

        $('#addButton').modal('hide');
        $(this).removeData('#addButton.modal');
    }
})

@Jeremy-s

I couldn't get your suggestion to work, although the Session key suggestion got me a solution that did for work with Bootstrap 2 but not with Bootstrap 3. I did finally get things working with Bootstrap 3, although I doubt it is the most elegant solution. I added ids to the elements and just set the value for each element to null like so:

$('#addButton').modal('hide')  
$('#buttonOrder').val(null);  
$('#buttonName').val(null);  
$('#buttonDescription').val(null);

Now I just need to figure out why I can't get it to work with the Session key and Bootstrap 3.

Jeremy S.

One possible way to go is to use Meteor's findAll to find the inputs in the template, then iterate through them and set the value of each input to null. The findAll is a jquery selector but is limited here to the context of the template.

Template.addButton.events({
  'submit form': function(e) {
    e.preventDefault();

    var button = {
      buttonOrder: $(e.target).find('[name=buttonOrder]').val(),
      buttonName: $(e.target).find('[name=buttonName]').val(),
      buttonDescription: $(e.target).find('[name=buttonDescription]').val()
    }

    button._id = Buttons.insert(button);

    _.each(
      this.findAll("input"),
      function(element){element.value = null}
    );

    $('#addButton').modal('hide');
  }
})

Note also that instead of using jquery to show and hide the modal, Meteor's preferred approach would be to add and remove it completely from the DOM based on a reactive source like a Session key. I would look at Meteor's reactivity summary for additional guidance on how to do this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to clear FormData() after submitting

How clear bootstrap modal on hide

Clear Redux-Form Fields after submitting

how to keep values in textfields after submitting a form in laravel 5.2

How to prevent the modal from closing after submitting form

How to clear input field after submitting data in angular?

How to close popup after submitting values in react native?

I want to open modal after submitting form?

retaining values after submitting

Clear form input field values after submitting in react js with ant-design

how to clear validation errors for mat error after submitting the form

React: How to clear file input and data input fields after submitting form?

How can I get the form values to appear in the table after submitting?

Jquery modal form clear values

Show bootstrap popover after submitting form that is in modal

How to retain values from textarea after submitting the form

How can I clear form values after successful form submission

php - How to keep form values after submitting to another website?

clear form values submitting through ajax

How to open a bootstrap modal box after submitting a form by barcode scanner

How to clear input field after submitting to database

How to display a bootstrap modal on submitting a form?

How to reset a bootstrap modal after submitting the form in jquery?

How to clear react state in modal after closing?

jQuery modal not showing after submitting form

how clear form filed after submitting data in flutter?

Unable to clear the form after submitting

How to clear field when submitting form

Retrieving values after submitting fetcher

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive