Python Flask wtforms disable form input field if checkbox is ticked

Viewed 35

Im struggling to get this to work.

I've got a python flask form that has a check box and an input field. I want to disable the input field if the check box is ticked. I've tried using a script in the HTML template but it doesn't seem to work, so I'm not sure what I'm doing wrong.

<div class="mb-3">
  {{ rent_add_form.full_amount_received.label }}
  {{ rent_add_form.full_amount_received }}
</div>
<div class="mb-3">
  {{ rent_add_form.amount_received.label}}
  {{ rent_add_form.amount_received }}
</div>

the full_amount_received is the checkbox, and the amount_received is the input field.

at the bottom of the HTML page I've got the following script

<script>
    function myFunction() {
        console.log
        // Get the checkbox
        var checkBox = document.getElementById("full_amount_received");

        // If the checkbox is checked, display the output text
        if (checkBox.checked == true){
            document.getElementById("amount_received").disabled = true;
        } else {
            document.getElementById("amount_received").disabled = false;
        }
    }
  </script>

if I check in chrome on developer tools, the ID's match up with what I've specified, but the console shows nothing when checking or unchecking the box.

1 Answers

i figured this out, i had to add render_kw={"onclick": "disableTextBox()"} to the properites in the form field within the form.py file

Related