Skip to content

.bootstrapToggle('off') not working from change event #226

Open
@BobWarren

Description

@BobWarren

Hi,
Despite a few dozen attempts several different ways I cannot get either the .bootstrapToggle('off') or .prop('disabled', false).change() to work.

The scenario:
We have a web form with 2 notification toggles - notify managers and notify everyone. If someone toggles the everyone button then it should turn off the manager toggle and disable it.

_Layout._html
Added both:

<script defer type="text/javascript" src="~/lib/bootstrap-toggle/js/bootstrap-toggle.js"></script>
$("input[type='checkbox']").bootstrapToggle();

The form:

<tr>
    <td class="FormLabelColumn">
        <label asp-for="NotifyManagers" class="control-label"></label>
    </td>
    <td class="FormLoaderColumn"></td>
    <td class="FormValueColumn">
        <input id="NotifyManagers" asp-for="NotifyManagers" type="checkbox"
               data-toggle="toggle" data-width="100" data-height="36"
               data-on="<span class='far fa-check fa-fw'></span>On"
               data-off="<span class='far fa-times fa-fw'></span>Off"
               data-onstyle="success" data-offstyle="secondary" />
    </td>
    <td class="FormButtonColumn"></td>
    <td class="FormExtraColumn">
        <span asp-validation-for="NotifyManagers" class="text-danger"></span>
    </td>
</tr>
<tr>
    <td class="FormLabelColumn">
        <label asp-for="NotifyEveryone" class="control-label"></label>
    </td>
    <td class="FormLoaderColumn"></td>
    <td class="FormValueColumn">
        <input id="NotifyEveryone" asp-for="NotifyEveryone" type="checkbox"
               data-toggle="toggle" data-width="100" data-height="36"
               data-on="<span class='far fa-check fa-fw'></span>On"
               data-off="<span class='far fa-times fa-fw'></span>Off"
               data-onstyle="success" data-offstyle="secondary" />
    </td>
    <td class="FormButtonColumn"></td>
    <td class="FormExtraColumn">
        <span asp-validation-for="NotifyEveryone" class="text-danger"></span>
    </td>
</tr>

The Javascript:

// Function to handle changes in NotifyEveryone toggle
function ValidateNotifyEveryone() {
    // If on
    if ($("#NotifyEveryone").is(':checked')) {
        // alert('NotifyEveryone on'); // CONFIRMED working
        // If manager field is checked then uncheck it
        if($("#NotifyManager").is(':checked')) {
            $("#NotifyManager").bootstrapToggle('off');
        }
        // Disable the manager field
        $("#NotifyManager").prop('disabled', false).change(); // also tried .bootstrapToggle('disable');
    }
    else {
        // alert('NotifyEveryone off'); // CONFIRMED working
        // Enable the manager field
        $("#NotifyManager").prop('disabled', true).change(); // also tried .bootstrapToggle('enable');
    }
}
// When document is fully loaded
$(document).ready(function () {
    // When the NotifyEveryone toggle is changed
    $("#NotifyEveryone").on('change', function () {
        // Validate
        ValidateNotifyEveryone();
    });

Thanks in advance for all your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions