Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
 }); 

}); 
like image 734
Maduro Avatar asked Jan 28 '16 17:01

Maduro


People also ask

How do I close a Bootstrap modal?

Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time.

How do I close modal backdrop?

Modals are built with HTML, CSS, and JavaScript. They're positioned over everything else in the document and remove scroll from the <body> so that modal content scrolls instead. Clicking on the modal “backdrop” will automatically close the modal.

How do you close a model on outside click?

Modal Header You have two options to close this modal: Click on the "x" or click anywhere outside of the modal!


2 Answers

HTML

<div id="someModalId" 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 id="exampleSelect">
    </select>
  </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() {
  $select_value = $("#exampleSelect").value();
  $('#someModalId').modal('hide');
});
  1. Give your select a name or id
  2. Set the value of select to a global variable. Make sure to declare the variable at the top if you are getting an error.
like image 180
Eshwaren Manoharen Avatar answered Sep 21 '22 01:09

Eshwaren Manoharen


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();

});
like image 30
Asenar Avatar answered Sep 19 '22 01:09

Asenar