I am trying to create a single mast switch that controls all the flags of the state child switch
Code
$(function() {
if (Array.prototype.forEach) {
var elems = Array.prototype.slice.call(document.querySelectorAll('.switches'));
elems.forEach(function(html) {
var switcherys = new Switchery(html);
});
}
else {
var elems = document.querySelectorAll('.switches');
for (var i = 0; i < elems.length; i++) {
var switcherys = new Switchery(elems[i]);
}
}
});
var MasterCheckbox = document.querySelector('.special');
MasterCheckbox.onchange = function () {
if (MasterCheckbox.checked) {
var special = document.querySelector('.chkChange');
special.checked = true;
if (typeof Event === 'function' || !document.fireEvent) {
var event = document.createEvent('HTMLEvents');
event.initEvent('change', true, true);
special.dispatchEvent(event);
} else {
special.fireEvent('onchange');
}
} else {
var special = document.querySelector('.chkChange');
special.checked = false;
if (typeof Event === 'function' || !document.fireEvent) {
var event = document.createEvent('HTMLEvents');
event.initEvent('change', true, true);
special.dispatchEvent(event);
} else {
special.fireEvent('onchange');
}
}
};
<link rel="stylesheet" href="http://abpetkov.imtqy.com/switchery/dist/switchery.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="http://abpetkov.imtqy.com/switchery/dist/switchery.min.js"></script>
<table>
<tr>
<td>Master Switch</td>
<td>
<input type="checkbox" class="switches special" />
</td>
</tr>
<tr>
<td>Child Switch</td>
<td>
<input type="checkbox" class="switches chkChange" />
</td>
</tr>
<tr>
<td>Child Switch</td>
<td>
<input type="checkbox" class="switches chkChange" />
</td>
</tr>
<tr>
<td>Child Switch</td>
<td>
<input type="checkbox" class="switches chkChange" />
</td>
</tr>
</table>
Run code- One Master switchery checkbox to control all child switches.
- If one child switch device of the state switch of the
enablemain switch switches to enable, if disabled, otherwise remainsdisable
The problem is that it was not possible to change the state of all child switches, but only one, you can check the code fragment
Plugin site
Github
Working script example
source
share