Search code examples
javascriptsymfonylistenerdom-events

symfony event listener on change


My form contains a select field that contains two options: show and hide option: I want when I select the show option, a text field should be appear in the form and if I select hide option, the text field should be disappear (hidden).

I ask which method should be used, any one has an example how doing this?


Solution

  • You certainly need Javascript to achieve this. Very simple working example using jQuery :

    $(function() {
      $('#type').change(function() {
        if ($('#type').val() == 'show') {
          $('#hidden_text').show();
        } else {
          $('#hidden_text').hide();
        }
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    Type
    <select name="type" id="type" style="margin-left:57px; width:153px;">
      <option name="Show" value="show">Show</option>
      <option name="Hide" value="hide">Hide</option>
    </select>
    
    <div class="row" id="hidden_text">
      Hidden text
    </div>

    You may want to adapt this example to the ids used in your view so that the onChange event is triggered on your select field.