Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How clear bootstrap modal on hide

How to can you clear the bootstrap modal on dismiss/hide/close?

I have the following Modal definition:

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
   Add New Comment
</button>

Partial view which contains Modal

@Html.Partial("_CreateComment", Model)


 // Partial view which contains modal

 <div class="modal fade" id="myModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
      @using (Ajax.BeginForm("AddComment", "Blog", new AjaxOptions
            {
                HttpMethod = "POST",
                InsertionMode = InsertionMode.Replace,
                UpdateTargetId = "comments",
                OnSuccess = "$('#myModal').modal('hide');"

            }))
      {
        <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">Add Comment</h4>
        </div>
        <div class="modal-body">
                @Html.ValidationSummary(true)
                @Html.HiddenFor(model => model.Blog.BlogID)

                <div class="form-group">
                    @Html.LabelFor(model => model.BlogComment.Comment)
                    @Html.TextAreaFor(model => model.BlogComment.Comment, 4, 104, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.BlogComment.Comment)
                </div>

        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-primary">Save changes</button>
            <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
        </div>
      }
  </div>
   </div>
</div>

And this is the javascript I am using to to clear the content:

$(function () {
    //clear modal cache, so that new content can be loaded
    $('body').on('hidden.bs.modal', '.modal', function () {
        $(this).removeData('bs.modal');

    });
});

If I dismiss the modal after having entered some content or upon submission the content in the form doesn't clear?

like image 259
adam78 Avatar asked Jun 24 '15 09:06

adam78


People also ask

How do I delete modal data?

modal', function () { $(this). removeData('bs. modal'); }); body ... ... This tells Bootstrap to clear everything on the close of the modal window, so you won't get cached content.

How do I dispose of Bootstrap modals?

Live Demo: Dispose Bootstrap Modal Using jQuery. First launch modal, then close it, then press "Dispose Modal" button to remove the modal data stored on the DOM element. Press Ctrl+Shift+J (Windows / Linux) or Cmd+Opt+J (Mac) to open the browser console panel.

How do you destroy modals?

modal('dispose') is a function defined to destroy the modal. The modal remains a part of the DOM even after using . modal('dispose'), this function only destroys the current instance of the modal component.


2 Answers

this is the easiest fix:

$('#myModal').on('hidden.bs.modal', function () {
    $(this).find("input,textarea,select").val('').end();

});
like image 71
adam78 Avatar answered Sep 19 '22 14:09

adam78


Use val('') based on input types present and use #myModal instead of body

$('#myModal').on('hidden.bs.modal', function () {
        $('.modal-body').find('textarea,input').val('');
});

DEMO

like image 22
Guruprasad J Rao Avatar answered Sep 18 '22 14:09

Guruprasad J Rao