still trying to style checkboxes
parent
be25603bd4
commit
75d2fe5442
@ -1,64 +1,79 @@
|
|||||||
(function () {
|
(function () {
|
||||||
// your page initialization code here
|
// your page initialization code here
|
||||||
// the DOM will be available here
|
// the DOM will be available here
|
||||||
|
|
||||||
const toggler = document.querySelector(".toggler");
|
const toggler = document.querySelector(".toggler");
|
||||||
console.log(toggler);
|
console.log(toggler);
|
||||||
toggler.addEventListener(
|
toggler.addEventListener(
|
||||||
"click",
|
"click",
|
||||||
function (e) {
|
function (e) {
|
||||||
const element = document.querySelector(".navbar");
|
const element = document.querySelector(".navbar");
|
||||||
element.classList.toggle("active");
|
element.classList.toggle("active");
|
||||||
},
|
},
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
|
||||||
const isDark = localStorage.getItem("isDark");
|
const isDark = localStorage.getItem("isDark");
|
||||||
if (isDark == 1) {
|
if (isDark == 1) {
|
||||||
|
const element = document.querySelector("body");
|
||||||
|
element.classList.add("dark");
|
||||||
|
}
|
||||||
|
const themetoggler = document.querySelector(".themetoggler");
|
||||||
|
themetoggler.addEventListener(
|
||||||
|
"click",
|
||||||
|
function (e) {
|
||||||
|
e.preventDefault();
|
||||||
const element = document.querySelector("body");
|
const element = document.querySelector("body");
|
||||||
element.classList.add("dark");
|
element.classList.toggle("dark");
|
||||||
}
|
|
||||||
const themetoggler = document.querySelector(".themetoggler");
|
const isDark = localStorage.getItem("isDark");
|
||||||
themetoggler.addEventListener(
|
localStorage.setItem("isDark", isDark == 1 ? 0 : 1);
|
||||||
"click",
|
},
|
||||||
function (e) {
|
false
|
||||||
e.preventDefault();
|
);
|
||||||
const element = document.querySelector("body");
|
|
||||||
element.classList.toggle("dark");
|
document.addEventListener("DOMContentLoaded", function() {
|
||||||
|
// Move all your code here
|
||||||
|
|
||||||
const isDark = localStorage.getItem("isDark");
|
var mutationObserver = new MutationObserver(function(mutations) {
|
||||||
localStorage.setItem("isDark", isDark == 1 ? 0 : 1);
|
mutations.forEach(function(mutation) {
|
||||||
},
|
var checkboxes = document.querySelectorAll('.cbi-checkbox input[type="checkbox"]');
|
||||||
false
|
|
||||||
);
|
checkboxes.forEach(function(checkbox) {
|
||||||
var checkboxes = document.querySelectorAll('.cbi-checkbox input[type="checkbox"]');
|
// Check if toggle switch is already created
|
||||||
|
if (!checkbox.nextSibling || checkbox.nextSibling.className !== 'switch') {
|
||||||
checkboxes.forEach(function(checkbox) {
|
var switchElement = document.createElement('label');
|
||||||
var switchElement = document.createElement('label');
|
switchElement.className = 'switch';
|
||||||
switchElement.className = 'switch';
|
|
||||||
|
var input = document.createElement('input');
|
||||||
var input = document.createElement('input');
|
input.type = 'checkbox';
|
||||||
input.type = 'checkbox';
|
|
||||||
input.checked = checkbox.checked;
|
|
||||||
switchElement.appendChild(input);
|
|
||||||
|
|
||||||
var slider = document.createElement('span');
|
|
||||||
slider.className = 'slider';
|
|
||||||
switchElement.appendChild(slider);
|
|
||||||
|
|
||||||
// Add the switch to the div wrapping the checkbox
|
|
||||||
checkbox.parentNode.insertBefore(switchElement, checkbox);
|
|
||||||
|
|
||||||
// Hide the original checkbox
|
|
||||||
checkbox.style.display = 'none';
|
|
||||||
|
|
||||||
input.addEventListener('change', function() {
|
|
||||||
checkbox.checked = this.checked;
|
|
||||||
});
|
|
||||||
|
|
||||||
var observer = new MutationObserver(function() {
|
|
||||||
input.checked = checkbox.checked;
|
input.checked = checkbox.checked;
|
||||||
|
switchElement.appendChild(input);
|
||||||
|
|
||||||
|
var slider = document.createElement('span');
|
||||||
|
slider.className = 'slider';
|
||||||
|
switchElement.appendChild(slider);
|
||||||
|
|
||||||
|
// Add the switch to the div wrapping the checkbox
|
||||||
|
checkbox.parentNode.insertBefore(switchElement, checkbox);
|
||||||
|
|
||||||
|
// Hide the original checkbox
|
||||||
|
checkbox.style.display = 'none';
|
||||||
|
|
||||||
|
input.addEventListener('change', function() {
|
||||||
|
checkbox.checked = this.checked;
|
||||||
|
});
|
||||||
|
|
||||||
|
var observer = new MutationObserver(function() {
|
||||||
|
input.checked = checkbox.checked;
|
||||||
|
});
|
||||||
|
observer.observe(checkbox, { attributes: true });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
observer.observe(checkbox, { attributes: true });
|
});
|
||||||
});
|
});
|
||||||
})();
|
|
||||||
|
mutationObserver.observe(document.documentElement, { childList: true, subtree: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
})(); // Make sure to include this closing partz
|
Loading…
Reference in New Issue