Search code examples
javascriptjquerytwitter-bootstrapbootbox

How can you pass a variable to Bootbox confirm?


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.


Solution

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