3

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.

Siguza
  • 21,155
  • 6
  • 52
  • 89
LBF
  • 1,133
  • 2
  • 14
  • 39

2 Answers2

2

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",
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • Do you know why the same method does not work inside the callback if you use $(this)? It seems $(this) is undefined because it is now referring to bootbox instead. Is there a way to redefine "this" inside the callback? – LBF Sep 07 '14 at 18:51
  • you have to store its reference in a var outside callback and use that reference – Ehsan Sajjad Sep 07 '14 at 18:52
  • I may need to open a new question. I tried defining `var Foo = $(this).attr('id');` before bootbox, but when I reference `Foo` inside the callback, it is undefined. – LBF Sep 07 '14 at 18:56
  • I updated my question to show the code. I appreciate you taking a look. – LBF Sep 07 '14 at 19:04
  • can you reproduce this in a jsfiddle? – Ehsan Sajjad Sep 07 '14 at 19:06
2

If you need to work with this variable in callback function, u can pass it by making "global", but in time between user call confirm and start to execute callback some other script can change it (or reverse), so for prevent conflicts desirable to create some object with methods to set, get variables and blocking mechanism that lock/unlock variable status on set/get events and execute all events not async.

var _this = this;
confirm({
    title: "Menu discard",
    message: 'Are you sure?',
    callback: function(resolve) {
        if(resolve) {
            // _this is accessible
        }
    }
});
entpnerd
  • 10,049
  • 8
  • 47
  • 68
Urgotto
  • 863
  • 7
  • 6