JSFiddle - React, Tailwind, and code Playground

by AlexP11223

HTML

<legend>Complete the recipe</legend>
    <div class="checkbox-group">
        <input type="checkbox" data-role="parent" name="EnchTbl" id="EnchTbl">
        <label for="EnchTbl">Enchantment table</label>
        <br>
        <span>
            <input type="checkbox" id="book" name="ingredient" value="book">
            <label for="book">Book</label>
            <br>
            <input type="checkbox" id="diamonds" name="ingredient" value="diamonds">
            <label for="diamonds">Diamonds (x2)</label>
            <br>
            <input type="checkbox" id="obsidian" name="ingredient" value="obsidian">
            <label for="obsidian">Obsidian (x4)</label>
        </span>
    </div>
    <legend>Complete the recipe 2</legend>
    <div class="checkbox-group">
        <input type="checkbox" data-role="parent" name="EnchTbl2" id="EnchTbl2">
        <label for="EnchTbl2">Enchantment table 2</label>
        <br>
        <span>
            <input type="checkbox" id="book2" name="ingredient" value="book">
            <label for="book2">Book</label>
            <br>
            <input type="checkbox" id="gold" name="ingredient" value="diamonds">
            <label for="gold">Gold</label>
        </span>
    </div>

JavaScript

function makeCheckboxGroup(container) {
    const parentCheckbox = container.querySelector('input[type="checkbox"][data-role="parent"]');
    const childrenCheckboxes = container.querySelectorAll('input[type="checkbox"]:not([data-role="parent"])');

    childrenCheckboxes.forEach(chk => chk.addEventListener('click', () => {
        const checkedCheckboxes = Array.from(childrenCheckboxes).filter(chk => chk.checked);

        parentCheckbox.checked = checkedCheckboxes.length > 0;
    }));

    parentCheckbox.addEventListener('click', () => {
        childrenCheckboxes.forEach(chk => {
            chk.checked = parentCheckbox.checked;
        });
    });
}

document.querySelectorAll('.checkbox-group').forEach(makeCheckboxGroup);