-1

I want to change the variable inside Twig by javascript, but there is an error: Variable "max" does not exist.

my twig:

{% block body %}
    <label for="maxN">Niveau MAX</label>
    <input type="number" id="maxN" class="form-control">

    {{ form_label(form.ncUoCommandeUo) }}
    {{ form_widget(form.ncUoCommandeUo, {'attr':{'min':1, 'max': max}}) }}
{% endblock %}

{% block javascripts %}
    <script>

      my.ajax......

      if (ncMaxUo.val() === 1) {
        max = 1;
      } elseif(ncMaxUo.val() === 2) {
        max = 2;
      } elseif(ncMaxUo.val() === 3) {
        max = 3;
      } else {
        max = 4;
      }
      ....
    </script>
{% endblock %}
Mikhail Prosalov
  • 4,155
  • 4
  • 29
  • 41
Amelie
  • 27
  • 5
  • sorry, ncMaxUo is maxN – Amelie Dec 22 '20 at 16:34
  • 2
    Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – DarkBee Dec 22 '20 at 16:37

1 Answers1

0

Just update my solution.

{% block body %}
<label for="maxN">Niveau MAX</label>
<input type="number" id="maxN" class="form-control">

{{ form_label(form.commande) }}
{{ form_widget(form.commande, {'attr':{'min':1}}) }}
{% endblock %}

{% block javascripts %}
<script>

 my.ajax......

var maxN = $("#maxN");
maxN = ???;(get value from ajax)

 $("#commande").attr("max", maxN.val());
....
</script>
{% endblock %}
Amelie
  • 27
  • 5