0

I'm new with bootstrap, and I've code for message confirmation.

How can I put my onclick() using this code below?

$("#myModal").on("show", function() {    // wire up the OK button to dismiss the modal when shown
    $("#myModal a.btn").on("click", function(e) {
        console.log("button pressed");   // just as an example...
        $("#myModal").modal('hide');     // dismiss the dialog
    });
});
$("#myModal").on("hide", function() {    // remove the event listeners when the dialog is dismissed
    $("#myModal a.btn").off("click");
});

$("#myModal").on("hidden", function() {  // remove the actual elements from the DOM when fully hidden
    $("#myModal").remove();
});

$("#myModal").modal({                    // wire up the actual modal functionality and show the dialog
  "backdrop"  : "static",
  "keyboard"  : true,
  "show"      : true                     // ensure the modal is shown immediately
});

Html of bootbox:

HTML (image)

onclick() input:

<input type='submit' name='actualiza_noticia' class='button' onClick="" value='Atualizar notícia'>    
3
  • add html code in snippet instead of image Commented Jul 11, 2016 at 11:50
  • Are you sure you want to remove the modal from the DOM once it has been shown? This means you wont be able to show it again, it's usually enough just to hide it. Commented Jul 11, 2016 at 12:03
  • Also you don't need the event listener for the close button as you have data-dismiss="modal" in your html for the close button. Commented Jul 11, 2016 at 12:04

2 Answers 2

1

You can use either use the jQuery function $(elem).modal('show') function, or the Bootstrap html data attributes:

Using data attributes:

<input type='submit' data-toggle="modal" data-target="#myModal" name='actualiza_noticia' class='button' value='Atualizar notícia' >    

Using Jquery function:

<input onclick="$('#myModal').modal('show')" type='submit' name='actualiza_noticia' class='button' value='Atualizar notícia' >    

These shuold both trigger your events, although the 'show' event is 'shown.bs.modal' to be in compliance with Bootstrap 3:

$("#myModal").on('shown.bs.modal', function() {    // wire up the OK button to dismiss the modal when shown
    $("#myModal a.btn").on("click", function(e) {
        console.log("button pressed");   // just as an example...
        $("#myModal").modal('hide');     // dismiss the dialog
    });
});

Read more about bootstrap modal here.

Sign up to request clarification or add additional context in comments.

2 Comments

It worked but now i have another problem? it seems to cancel the submit action of my form...better explaining ?
i used a while to print a lits o data to with updtade delete buttons and after submite then when to a checklogin.php area to check if choosed update or delete...it worked but with bootbox, it just shows or hides..
0

According to the image that reports your html my proposal is:

  • change $("#myModal").on("show", function() { to `$("#myModal").on("shown.bs.modal", function() {
  • change $("#myModal a.btn").on("click", function(e) { to $("#myModal button.button.btn.btn-primary").on("click", function(e) {
  • change $("#myModal").on("hide", function() { to $("#myModal").on("hide.bs.modal", function() {
  • change $("#myModal a.btn").off("click"); to $("#myModal button.btn.btn-primary").off("click");

For details see bootstrap modals

 $(function () {
            $("#myModal").on("shown.bs.modal", function() {    // wire up the OK button to dismiss the modal when shown
                $("#myModal button.btn.btn-primary").on("click", function(e) {
                    console.log("button pressed");   // just as an example...
                    $("#myModal").modal('hide');     // dismiss the dialog
                });
            });
            $("#myModal").on("hide.bs.modal", function() {    // remove the event listeners when the dialog is dismissed
                $("#myModal button.btn.btn-primary").off("click");
            });

            $("#myModal").on("hidden", function() {  // remove the actual elements from the DOM when fully hidden
                $("#myModal").remove();
            });

            $("#myModal").modal({                    // wire up the actual modal functionality and show the dialog
                "backdrop"  : "static",
                "keyboard"  : true,
                "show"      : true                     // ensure the modal is shown immediately
            });
        });
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<div id="myModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <!-- dialog box -->
            <div class="modal-body">
                <button type="btn btn-default" class="close" data-dismiss="modal">&times;</button>
                Hello world!
            </div>
            <!-- dialog buttons -->
            <div class="modal-footer">
                <button type="btn btn-default" class="btn btn-primary">OK</button>
            </div>
        </div>
    </div>
</div>

1 Comment

It worked but now i have another problem? it seems to cancel the submit action of my form...better explaining i used a while to print a lits o data to with updtade delete buttons and after submite then when to a checklogin.php area to check if choosed update or delete...it worked but with bootbox, it just shows or hides..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.