How can you pass a variable to Bootbox?

Is it possible to pass jQuery variable in bootbox.js confirm box?

Here is the code:

bootbox.confirm({
message: 'Are you sure you want to remove [productName]?',
callback: function(result) {
    console.log(result);
},
title: "You can also add a title",
});

      

Where "productName" is a variable of type:

var productName = $('#product_name').val();

      

Is something like this possible?

UPDATE: Thanks for the answer below!

Going into this question .... if I had something like this:

$('#product_name1 a.trash').click(function(event){

event.stopImmediatePropagation();           
event.preventDefault();

var foo = $(this).parent().attr('id');  // #product_name1

alert(foo); // correct!

bootbox.confirm({
message: 'Are you sure you want to remove [productName]?',
callback: function(result) {
   if (confirmed) 
    alert(foo); // undefined!
   }
}

});

});

      

How can I override "this" inside a callback? "foo" returns undefined because it now refers to the bootbox.

+3


source to share


2 answers


Yes, you can just concatenate the value using the message string:

var productName = $('#product_name').val();

bootbox.confirm({
message: 'Are you sure you want to remove'+productName,
callback: function(result) {
    console.log(result);
},
title: "You can also add a title",

      



or cleaner:

var productName = $('#product_name').val();
var Message = 'Are you sure you want to remove'+productName;

    bootbox.confirm({
    message: Message ,
    callback: function(result) {
        console.log(result);
    },
    title: "You can also add a title",

      

+2


source


If you need to work with this variable in a callback function, you can pass it by making it "global", but over time between confirming the custom call and starting the callback, some other script may change it (or vice versa) so it is advisable to create any object with set methods, get variables and a locking mechanism that lock / unlock the status of a variable in set / get events and execute all non-async events.



var _this = this;
confirm({
    title: "Menu discard",
    message: 'Are you sure?',
    callback: function(resolve) {
        if(resolve) {
            // _this is accessible
        }
    }
});

      

+1


source







All Articles