JSFiddle - React, Tailwind, and code Playground
by LuckyCat
HTML
<div class="checkbox-block">
<div class="checkbox-item tr-option">
Test text 1
</div>
<div class="checkbox-item tr-option">
Test text 2
</div>
<div class="checkbox-item tr-option">
Test text 3
</div>
<div class="checkbox-item tr-option">
Test text 4
</div>
</div>
CSS
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
:focus {
outline: 0;
}
img {
border: none;
display: block;
}
ol,
ul {
list-style: none;
}
a {
text-decoration: none;
cursor: pointer !important;
}
html {
font-size: 62.5%;
-webkit-text-size-adjust: 100%;
position: relative;
}
body {
font-family: Arial, sans-serif;
color: #333;
line-height: 1.3;
font-size: 1.6rem;
}
.checkbox-block {
margin-top: 2rem;
}
.checkbox-item {
position: relative;
padding: 0 0 0 4.5rem;
margin-bottom: 2rem;
text-align: left;
line-height: 3rem;
cursor: pointer;
transition: all 0.3s linear;
}
.checkbox-item::before {
content: "";
position: absolute;
width: 3rem;
height: 3rem;
border: 1px solid #d3cfc8;
top: 0;
left: 0;
line-height: 2.2rem;
color: #fff;
font-family: Arial, sans-serif;
font-weight: bold;
font-size: 1.6rem;
text-align: center;
}
.checkbox-item.checked::before {
background-color: #de9c7d;
}
.checkbox-item.checked::after {
content: '';
display: block;
position: absolute;
top: 0.1rem;
left: 0.9rem;
border: 0.6rem solid #fff;
border-left: 0;
border-top: 0;
width: 0.8rem;
height: 1.7rem;
transform: rotate(45deg);
}
JavaScript
/*var $checkbox = [...document.getElementsByClassName('checkbox-item')];*/
var $checkbox = [].slice.call(document.getElementsByClassName('checkbox-item'));
var counter = 0;
//var siblings = 0;
/*function getChildrens(n, selector) {
var nodes = [];
while (n.nextElementSibling != null) {
if (n.nextElementSibling.hasOwnProperty('classList')) {
if (n.nextElementSibling.classList.contains(selector)) {
//return n.nextElementSibling;
nodes.push(n.nextElementSibling);
}
}
n = n.nextElementSibling;
}
return nodes;
}
function getSiblings(n, selector) {
return getChildrens(n, selector);
}
function toggleTip(elem) {
var siblings = getSiblings(elem, "tip-content");
if (siblings.length > 0) {
for (var i = 0; i < siblings.length; i++) {
siblings[i].classList.toggle("hidden");
}
}
}*/
function findSiblingWithId(element) {
var siblings = element.parentNode.children,
sibWithId = null;
for(var i = siblings.length; i--;) {
if(siblings[i].id) {
console.log('sibWithId');
sibWithId = siblings[i];
break;
}
}
if(sibWithId) {
// found it
console.log('sibWithId');
}
}
// checkbox.forEach((indexButton, index) =>{
var counter = 0;
$checkbox.forEach(indexButton =>{
indexButton.addEventListener('click', index => {
/*var siblings = getSiblings(this, "checked");
console.log(siblings);
if (siblings.length > 0) {
for (var i = 0; i < siblings.length; i++) {
siblings[i].classList.toggle("hidden");
}
}*/
var isThirdIteration = (counter + 1) % 3 === 0;
console.log(isThirdIteration);
if (isThirdIteration) {
//this.className += ' newClassName';
console.log('if: ' + isThirdIteration);
}
counter++;
['checked', 'tr-selected'].map(className => indexButton.classList.toggle(className));
// findSiblingWithId(indexButton);
//...