why my modal is not closing on clicking add area button

Viewed 56

on click add area button my modal should be closed but it is not working. everything looks fine but i can not find error. id="category-popup" should close modal by $("#category-popup").modal('hide'); but it is not working can someone help me?

 <div class="modal fade" id="category-popup" tabindex="-1" role="dialog" aria-labelledby="category- 
   popup" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title">Add Area</h5>
            <button class="close" type="button" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">×</span>
            </button>
          </div>
          <div class="modal-body">
            <form method="post" action="{{ route("save.area.ajax") }}" class="category-form">
              {{ csrf_field() }}
              <div class="form-group">
                <label>Area Name</label>
                <input type="text" name="name" class="form-control old-category" required="">
              </div>
              <div class="form-group">
                <button type="button" class="btn btn-primary btn-block add-area-submit-btn">Add 
                 Area</button>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>

java script

$('.add-category').click(function(event){
    event.preventDefault();
    $('.old-category').val('')
    $('.category-form').attr('action' , '{{ route("save.area.ajax") }}');
    $('#category-popup .modal-title').html('Add Area');
    $('#category-popup .modal-footer button').text('Add Area');
  });
  $(document).on('click', '.add-area-submit-btn', function(e){
    e.preventDefault();
    var btn = $(this);
    if($('.category-form .old-category').val() != ""){
      $.ajax({
        type: 'post',
        data: $('.category-form').serialize(),
        url: $('.category-form').attr('action'),
        success: function(res){
          $("#area-select").html(res);
          $("#category-popup").modal('hide');
        }
      });
    }
  });
3 Answers

The syntax for hiding modal is correct but please make sure that your $.ajax request is succeeding. Try doing console.log() in $.ajax success. Or add error in your ajax so you could know the error

$.ajax({
    type: 'post',
    data: $('.category-form').serialize(),
    url: $('.category-form').attr('action'),
    success: function(res) {
        console.log("ajax request success");
        $("#area-select").html(res);
        $("#category-popup").modal('hide');
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log("ajax request failed: " + errorThrown);
    }
});

Hm, weird, are you sure you include jQuery correctly? Could you show your included scripts?
jQuery should be like: <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

In JavaScript one error results in broking whole script. And without jQuery your "modal" action will not work. I think it's the reason why your script not working.

enter image description here

check errors in the cnsole it submit area in option field but not closed

Related