aanders77
aanders77

Reputation: 630

JQuery UI dialog - How do I add buttons from an object

I have this code:

var bt = {}

bt = {
    text: "OK",
    click: function () {
        // Deletefunction here...
        $(this).remove()
    }
}, {
    text: "Cancel",
    click: function () {
        $(this).remove()
    }
}

$("#test").dialog({
    title: "Confirm deletion",
    width: "300px",
    buttons: [bt]
})

Why do I only see the OK button, and not the Cancel button?? JSfiddle here.

Upvotes: 0

Views: 245

Answers (1)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262919

That's because your assignment to bt does not do what you think it does. It ends up applying the comma operator and is equivalent to:

bt = {
    text: "OK",
    click: function () {
        // Deletefunction here...
        $(this).remove();
    }
};
// Stray, ignored object literal.
{
    text: "Cancel",
    click: function () {
        $(this).remove();
    }
};

If you want bt to be an array, then make it so:

var bt = [{
    text: "OK",
    click: function () {
        // Deletefunction here...
        $(this).remove();
    }
}, {
    text: "Cancel",
    click: function () {
        $(this).remove();
    }
}];

$("#test").dialog({
    title: "Confirm deletion",
    width: "300px",
    buttons: bt
});

Upvotes: 1

Related Questions