1

I have a Flask form (via WTForms) in my website, and am trying to enable or disable other fields in my form based on whether a user has selected or de-selected a checkbox (and do this without having to refresh the page) however, my attempts to achieve this by using the onchange or onclick event in the HTML hasn't worked. My field is defined below (label and field)

<div class="form-group row">
  {{ form.task_submit_sms.label(class_="col-sm-3 text-right control-label col-form-label") }}
  <div class="col-sm-1">
      {{ form.task_submit_sms(class_="custom-control custom-checkbox" onclick="enableSMSFields()") }}
  </div>

I have tried both onclick and onchange. and get the following error:

jinja2.exceptions.TemplateSyntaxError: expected token ',', got 'onclick'

Not sure if it's relevant, but here's my JavaScript:

<script>
  // Lock SMS fields unless required
  function enableSMSFields()
  {
    if (document.getElementById('task_submit_sms').checked == true)
    {
      document.getElementById('task_sms_text').removeAttribute('disabled');
    }
    else
    {
      document.getElementById('task_sms_text').setAttribute('disabled','disabled')
    }
  }
</script>

1 Answer 1

5

This is a function inside the curly brackets, which you are passing arguments separated by commas (that is what the error is saying to you):

{{ form.task_submit_sms(class_="custom-control custom-checkbox" onclick="enableSMSFields()") }}

Have you tried inserting the comma separator?

{{ form.task_submit_sms(class_="custom-control custom-checkbox", onclick="enableSMSFields()") }}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.