user3354539 user3354539 - 28 days ago 5
Javascript Question

bootstrap-switch - trigger event when toggled

I have installed bootstrap-switch on a page to replace a HTML checkbox with a toggle element.

The replacement works well, but I have an issue getting the code syntax correct to trigger a function when the checkbox is changed / toggled.

I have seen this post, but my code skills are not so great that I can solve this issue.

Here is my current code that works when I do not install the bootstrap-switch code on the

getAddressDetailsFormValues
function:

$(document).ready(function() {
....
$('#id_address_style_one_line').bootstrapSwitch({'labelWidth': '5px', 'offText': '{% trans "No" %}', 'onText': '{% trans "Yes" %}', 'size': 'mini'});
....
});

function getAddressDetailsFormValues() {
return {
....
'address_style_one_line':
($('#id_display_address_on_one_line').is(':checked')) ?
true:
false,
....
};
}


I have read the docs, but I cannot get the correct code syntax to trigger the change with the bootstrap-switch on the
getAddressDetailsFormValues
function.

Answer

Try replacing the following:

($('#id_display_address_on_one_line').is(':checked')) ?
                true:
                false,

with:

$('#id_display_address_on_one_line').bootstrapSwitch('state')
Comments