最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Bootbox - how to dynamically change message content? - Stack Overflow

matteradmin3PV0评论

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

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

Share asked Sep 11, 2015 at 14:43 Mirco LclMirco Lcl 3737 silver badges19 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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");

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]");

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

#jQuery
$('.modal-title').html('New Title');
$('.modal-body').html('New Message');
Post a comment

comment list (0)

  1. No comments so far