1

I have the modal windows on bootstrap and JQuery code to work this some values.

The question is how to open this modal window by pressing to process button?

<button type="button" class="btn btn-primary" id="#exampleModalCenter" onclick="process();">
    Process
</button>

<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p class="form-control" id="mySelectedValue" style="margin-top: 10px;"></p>
        <button class="btn btn-primary send" style='font-family: "Lucida Grande", "Lucida Sans Unicode", Arial, Helvetica, Verdana, sans-serif; font-size: 14px;"'>
            Send
        </button>
      </div>
    </div>
  </div>
</div> 

jquery

<script type="text/javascript">
function printValue(selectedItem) {
    $('#mySelectedValue').html(selectedItem.value);
    console.log(selectedItem.value);
}
function process(selectedItem) {
    document.getElementById('#exampleModalCenter')
    const data = JSON.stringify({
        "selectedItems": $('#sel').val()
    });
    $.ajax({
        url: "/",
        type: "POST",
        contentType: "application/json",
        data: data,
        success: function (data) {
          console.log(data);
        },
    });
}
function optionClick(selectedItem) {
    printValue(selectedItem);
}
</script>   
  • Took less than 10 seconds to find [tons of dupes](https://www.google.com/search?q=bootstrap+open+modal+click+site:stackoverflow.com) – mplungjan Jul 05 '20 at 08:48

1 Answers1

2

Open modal by jQuery

function printValue(selectedItem) {
    $('#mySelectedValue').html(selectedItem.value);
    console.log(selectedItem.value);
}
function process(selectedItem) {
    $('#exampleModalCenter').modal('show')
    document.getElementById('#exampleModalCenter')
    const data = JSON.stringify({
        "selectedItems": $('#sel').val()
    });
    $.ajax({
        url: "/",
        type: "POST",
        contentType: "application/json",
        data: data,
        success: function (data) {
          console.log(data);
        },
    });
}
function optionClick(selectedItem) {
    printValue(selectedItem);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<button type="button" class="btn btn-primary" id="#exampleModalCenter" onclick="process();">
    Process
</button>

<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p class="form-control" id="mySelectedValue" style="margin-top: 10px;"></p>
        <button class="btn btn-primary send" style='font-family: "Lucida Grande", "Lucida Sans Unicode", Arial, Helvetica, Verdana, sans-serif; font-size: 14px;"'>
            Send
        </button>
      </div>
    </div>
  </div>
</div>
glinda93
  • 7,659
  • 5
  • 40
  • 78