Enable input field oncheck using javascript

Can I make my code look like this? I am wondering if I can do this, as I am trying this from last 3 days but still no success. I want to enable a specific input field on check using javascript but it is not working.

<?php
    $rows = 1;

    if(isset($_GET['number_of_rows'])) {
        $rows = $_GET['number_of_rows'];
    }

    for ($i=0; $i < $rows; $i++) { 
    ?>

    <input type="checkbox" id="mycheck<?=$i?>" onclick="myFunction<?=$i?>()">Enable row
    <input placeholder="student name" type="text" name='name<?=$i?>' id="input<?=$i?>" disabled required>   
    <input placeholder="student id" type="number" name='id<?=$i?>' id="input<?=$i?>" disabled required>

    </div>

    <?php
    }
    ?>

    <form method="get">
        <input type="number" value="<?=$rows?>" max=10 min="1" name="number_of_rows" required>
        <input type="submit" name="submit2">
    </form>

    <?php for ($i=0; $i < $rows; $i++) { ?>
        <script>

        function myFunction<?=$i?>() {

            if(mycheck<?=$i?>.checked == true) {
                document.querySelectorAll("[id^='input<?=$i?>']")
                .forEach(element => 
                    element.disabled = false
                );
            }

            if(mycheck<?=$i?>.checked == false) {
                document.querySelectorAll("[id^='input<?=$i?>']")
                .forEach(element =>
                    element.disabled = true
                );
            }

            if(document.getElementById("mycheck<?=$i?>").disabled == true) {
                document.getElementById("mycheck<?=$i?>").disabled = false;
            }
        }

        </script>
    <?php } ?>