Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Clear fields when closing div

To open and close div I used the following code:

$('#addvisit').on('click', function(e) {
    e.stopImmediatePropagation();
  $('#fechvisit').slideToggle('slow');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<button type="button" id="addvisit" class="hs-button primary large">Adicionar Visitante</button>

<div id="fechvisit" style="display: none;">
  <div class="row">
    <div class="col-xs-12">
      <div class="form-group">
        <label for="titl"> <strong>NOME VISITANTE</strong></label>
        <select class="js-states form-control" name="titl" id="titl">
          <option></option>
          <option value="teste">teste</option>
          <option value="teste1">teste1</option>
        </select>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-6">
      <div class="form-group">
        <label for="contac"> <strong>CONTACTO VISITANTE</strong></label>
        <input type="text" name="contac" class="form-control" id="contac">
      </div>
    </div>
  </div>
</div>

The problem I have is the following:

If you open the div and place values ​​in the select or input, if you close the div again, the fields are not cleared. I intended that whenever I closed the div, it would clear the fields.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can you help?

>Solution :

You would need to detect if the div is open or closed, and then set the values to of the inputs to nothing.

$('#addvisit').on('click', function(e) {
  e.stopImmediatePropagation();
  $('#fechvisit').slideToggle('slow', function() {
    if (!$(this).is(":visible")) {
      $('#contac, #titl').val('');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<button type="button" id="addvisit" class="hs-button primary large">Adicionar Visitante</button>

<div id="fechvisit" style="display: none;">
  <div class="row">
    <div class="col-xs-12">
      <div class="form-group">
        <label for="titl"> <strong>NOME VISITANTE</strong></label>
        <select class="js-states form-control" name="titl" id="titl">
          <option></option>
          <option value="teste">teste</option>
          <option value="teste1">teste1</option>
        </select>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-6">
      <div class="form-group">
        <label for="contac"> <strong>CONTACTO VISITANTE</strong></label>
        <input type="text" name="contac" class="form-control" id="contac">
      </div>
    </div>
  </div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading