3

I was wondering if there is a way to dinamically update the content of a bootbox modal.

Example

bootbox.dialog({
        message: "Hi there",
        title: "My title",
        buttons: {
            main: {
                label: "dismiss",
                className: "btn-primary",
            }
        }
    });


    newMessage = "this is a new message"

Is there a way to replace that "Hi there" with the new string newMessage?

Thanks for any help or suggestion

Mirco Lcl
  • 373
  • 7
  • 19

3 Answers3

8

yes, you can change bootbox msg by adding id reference to the msg. Below is sample code for it.

    bootbox.dialog({
       message: "<span id='dynamicMsg'>Hi there</span>",
       title: "My title",
       buttons: {
        main: {
            label: "dismiss",
            className: "btn-primary",
        }
      }
    });

    //Add this line wherever you want to change msg
    $("#dynamicMsg").text("This is dynamic msg");
Akash
  • 559
  • 6
  • 17
2

Simple! Create a generic function:

function bootBoxModal(title, message, type) {
    bootbox.dialog({
        message: message,
        title: title,
        alertType: type,
        buttons: {
            main: {
                label: 'Fechar', className: 'btn-default'}
        }
    });
}

Call the function now:

bootBoxModal("Title message", 
             "Content your message", 
             "type [alert,danger,warning,success]");
Nunser
  • 4,512
  • 8
  • 25
  • 37
  • Why this is the accepted answer? It doesn't create another dialog everytime you call that function instead of updating the existing one? – auron344 Jun 23 '21 at 10:53
0

Another solution is just to replace the content directly, this example uses jQuery.

#jQuery
$('.modal-title').html('New Title');
$('.modal-body').html('New Message');
mistajolly
  • 441
  • 4
  • 5