Search code examples
bootbox

Pre-checked checkbox in Bootbox


I'm using Bootbox and want to have one of the check boxes pre-selected. For example:

bootbox.prompt({
    title: 'What would you like on your pizza?',
    inputType: 'checkbox',
    inputOptions: [
        {
            text: 'pepperoni',
            value: '1',
        },
        {
            text: 'mushrooms',
            value: '2',
        },
        {
            text: 'onions',
            value: '3',
        }
    ],
    callback: function (result) {
        console.log(result);
    };

Lets say that I want to have the first item in the array, which is pepperoni, selected as a default. Is it possible to do this using Bootbox? Thanks in advance.


Solution

  • You need to add the value option to prompt.

    For a single checked option (https://jsfiddle.net/j7wgncsf/):

    bootbox.prompt({
        title: 'What would you like on your pizza?',
        inputType: 'checkbox',
        value: '1', // <----- this
        inputOptions: [
            {
                text: 'pepperoni',
                value: '1',
            },
            {
                text: 'mushrooms',
                value: '2',
            },
            {
                text: 'onions',
                value: '3',
            }
        ],
        callback: function (result) {
            console.log(result);
        }
    });
    

    Or, to have multiple options checked, use an array (https://jsfiddle.net/yasok05p/):

    bootbox.prompt({
        title: 'What would you like on your pizza?',
        inputType: 'checkbox',
        value: ['1', '2'],// <----- this
        inputOptions: [
            {
                text: 'pepperoni',
                value: '1',
            },
            {
                text: 'mushrooms',
                value: '2',
            },
            {
                text: 'onions',
                value: '3',
            }
        ],
        callback: function (result) {
            console.log(result);
        }
    });