1

I use bootstrap-switch and have different buttons. Givs a change, that i can give other attributes as parameter like this sample:

<input type="checkbox" name="values[{$optionName}]" value="1" data-to-enable="values[optionName1],values[optionName5],values[optionName50],values[optionName100],values[optionName333]" />

I want, that this options are disabled on unchecked (or when it's unchecked on page load), and enable (or when it's checked on page load) on checked

  • Can you provide more HTML of what you want ? plus the javascript code you already tried ? And describe exactly what you want, it is hard to tell at this moment – Dipiks Jan 30 '17 at 09:54

1 Answers1

0

Here is the javascript code you want:

$(function() {

    var toggleSwitch = function(ev) {
        var _this = $(ev.currentTarget);
        var toChange = _this.data("to-enable").split(",");
        var state = _this.bootstrapSwitch("state");

        $("input.switch").each(function(el, index) {
            if($.inArray($(this).prop("name"), toChange) >= 0) {
                $(this).bootstrapSwitch("disabled", state);
            }
        });
    }       

    $("input").bootstrapSwitch();
    $("input[id^='switchHandler']").each(function(){
        $(this).bootstrapSwitch("state", $(this).data("checked"));
        $(this).on("switchChange.bootstrapSwitch", toggleSwitch);
        $(this).trigger("switchChange.bootstrapSwitch");
    });

});

And here is a working jsfiddle

Dipiks
  • 3,818
  • 2
  • 23
  • 39
  • That is great. Thank you. I have change it with this: $("[id^=switchHandler]").on("switchChange.bootstrapSwitch", function(){ that I can make different switchHandler. Great. Givs a change to change it that the script disable/enable the buttons and not checked/unchecked? –  Jan 30 '17 at 10:38
  • Q: You are great. Thank you. Givs a change, to check id="switchHandler" on pageload if it checked or unchecked to enable/disable the switch from data-to-enable? At this moment, when I checked the switchHandler on page load, the option data-to-enable is the wrong way –  Jan 30 '17 at 12:03
  • @Koda Updated again. Now when the page load, the switch are based on the initial value of `$("#toggleSwitch")`. If you want to invert this behaviour, just negate the `state` variable` (by doing this: `!_this.bootstrapSwitch("state")`) – Dipiks Jan 30 '17 at 12:36
  • Thank you. Last one. Sorry. With your code I have two problems: 1. Givs a change that value="1" not say if checked or not? For me its better with checked="checked" or data-is-checked="0/1". 2. In commend before, I have write you that I have change it to $("[id^=switchHandler]") becauce I have more than one switchhandler. But with this it don't work in your new sample –  Jan 30 '17 at 13:08
  • @Koda There you are – Dipiks Jan 30 '17 at 13:20
  • One bug: When I change to checked="false" or delete it, the checkbox is allready checked –  Jan 30 '17 at 13:23
  • @Koda Fixed. I've used data-attribute instead of checked property because of the behaviour of bootstrap-switch. – Dipiks Jan 30 '17 at 13:31
  • Sorry again. Extremly rare I have a name like this: . Do you see a way that the can disabled too, or is this too much? –  Jan 30 '17 at 13:48