jeudi 18 mai 2023

How to mark checkboxes in a list backward, numerically descending?

I am generating a list from a database using PHP. Each line is numbered in ascending order and contains a checkbox. If you select one, all checkboxes above it, numerically descending, should also be selected.

I have already tried various things, but it does not work. Here is the code in a shortened form:

<table id="mySEARCH" class="shoppinglist-content">
<!-- ******************************************************************************** shopping list beginn-->
    <!-- script test start -->
    <?php
        if(isset($_COOKIE['shoppinglist'])){
            $list = $_COOKIE['shoppinglist'];
            $stmt = $pdo->prepare("SELECT * FROM `$list`");
            $stmt->execute();
            $x = 0;
            foreach($stmt as $item)
            { $x++;?>
                <tr id="line<?php echo $x;?>" style="opacity: 1.0;">
                <td class="split-list"><input type="checkbox" id="split-list-<?php echo $x;?>" name="split-list" onchange="markAbove<?php echo $x;?>()" value="<?php echo $x;?>"></td>
                </tr>
                <script>
                    function markAbove<?php echo $x;?>(){
                        var choosedLine = document.querySelectorAll("input[type=checkbox]");
                        for(var i = <?php echo $x;?>; i > choosedLine.value; i--){
                            choosedLine.setAttribute("checked", "");
                        }
                        console.log(i);
                    }
                </script>
            <?php
            }
        }else {
/******************************************************************************** shopping list end****/
            ?><tr><td></td><td></td><td>Keine Items vorhanden</td><td></td><td></td><td></td><td></td></tr><?php
        }
    ?>
<!-- script test end -->
</table>

enter image description here

Does anyone have a tip on how this can work?




Aucun commentaire:

Enregistrer un commentaire