How can you pass a variable to Bootbox confirm?

LBF picture LBF · Sep 7, 2014 · Viewed 7.7k times · Source

Is it possible to pass a jQuery variable into a 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 like:

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

Is something like this possible?

UPDATE: Thanks for the answer below!

Tacking onto 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 do I redefine "this" inside the callback? "foo" returns undefined because it's referring to bootbox now.

Answer

Ehsan Sajjad picture Ehsan Sajjad · Sep 7, 2014

Yes you can, you just have to concatenate the value with 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 more clean:

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",