Skip to content
Advertisement

How to save and close a bootstrap modal?

This question have many answers out there but none of those answers solved my problem. I have a modal with some <select> tags. I am trying to save the values from the select options. after they click Save the modal should close but not submit the result yet because the user still need to review stuff after they click save on the modal.

HTML

 <!-- Modal -->
 <div id="1" class="modal fade" role="dialog">
 <div class="modal-dialog">

  <!-- Modal content-->
  <div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;       </button>
    <h4 class="modal-title">Modal Header</h4>
  </div>
  <div class="modal-body">
    //select tags go here
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default"    data-dismiss="modal">Close</button>
    <button id="save" onclick="save()" class="btn btn-width bkgrnd-cyan save-details" type="button" name="save-details">Save</button>
  </div>
</div>

JS

$('#save').click(function() {
$('#1').modal('hide');
 });

Since my code is really long, I just copied a modal from google but I added my Save button, so this is the real Save button that my modal is using.

Thanks

EDIT

I changed the id number to letters so now the code doesn’t include any id starting with number, thanks.

PS still need help =/

EDIT

I also tried this one and it did not work

$(document).ready(function(){

 $('#save').click(function(){
$('#modal_1').modal('hide');//modal_1 is the id 1
 }); 

}); 

Advertisement

Answer

I’m not sure the id=1 is fine, but you can use this code to simulate a click on the close button (or you can add an id or a class to it).

You may have to remove the onclick="save()"

data = {};
$("#save").on("click", function(e){
  e.preventDefault(); // prevent de default action, which is to submit
  // save your value where you want
  data.select = $("#exampleSelect").value();
  // or call the save function here
  // save();
  $(this).prev().click();

});
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement