Javascript in custom form field Symfony2

I have a form field template as follows:

{% block test_question_widget %}
    {% spaceless %}
        <div  {{ block('widget_container_attributes') }}>
            {% set type = type|default('hidden') %}
            <input type="{{ type }}" {{ block('widget_attributes') }} value="{{ value }}"/>
            <div class="visible-element"></div>
        </div>
    {% endspaceless %}
{% endblock %}

      

Suppose my div.visible element needs to behave as I define in the js file, i.e. change the value of my hidden input eg. depending on the position on the page.

  • I need to add a js file. Where is the best place to do this?
  • Let's say I have 100 such fields. How to add js only once?
+3


source to share


1 answer


  • The best practice is to add your javascript code to an external .js file

  • With jQuery, you can easily repeat the same JS code for all elements:

    $ ('visible element'). Each (function () {// your JS code});



0


source







All Articles