vsn4ik / bootstrap-checkbox

A checkbox component based on Bootstrap framework.
https://vsn4ik.github.io/bootstrap-checkbox/
MIT License
213 stars 61 forks source link

Checkbox won't render when using AJAX #27

Open Maelstorm1973 opened 7 years ago

Maelstorm1973 commented 7 years ago

I've ran into an interesting problem. I have found that when rendering the page with AJAX (which is used everywhere today), checkboxes within the newly rendered content still show as a checkbox and not a switch. Rendering can be done by either DOM object manipulation or by direct HTML injection into a DIV tag. I have found that using this code

<!-- Checkbox Boot Script -->
<script type="text/javascript">
    function featureCheckbox() {
        $(':checkbox').checkboxpicker();
    }
</script>

at the bottom of the HTML template page, and then calling the function featureCheckbox() as the last action in the AJAX response handler, I was able to get it to work. The documentation should be updated to indicate this little caveat.

This is within the AJAX response handler.

// Feature Activations
if (typeof ajaxResponseHandler == typeof featureCheckbox)
{
    featureCheckbox();
}

Another thing that I noticed is that all checkboxes must be rendered and on screen before calling the activation routine. Any checkboxes that are rendered after the activation routine will not get the new visual and will remain as an ordinary checkbox. The documentation should be updated to reflect this.

Anyways, this is a nice addon. Good Job!