Open and close textarea with the same button

Asked

Viewed 89 times

0

HTML of textarea with the open button:

<input type="button" name="abrir" class="botao">
<div id="comentario" style="display:none">
<textarea id="Observacao" name="Observacao" style="color: black;"></textarea>
</div>

To open the button I am using the following script:

$('input[name=abrir]').mousedown(function(e){ 
    e.preventDefault();
    if( e.button != 1 ) { 
      $('#comentario').show();
    }
    return false;
  }); 

But I intended that by clicking the button again to close.

1 answer

3


In place of .show use the .toggle,

Ve documentation here

take the example:

$('input[name=abrir]').mousedown(function(e) {
  e.preventDefault();
  if (e.button != 1) {
    $('#comentario').toggle();
  }
  return false;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="button" name="abrir" value='Abrir / Fechar' class="botao">
<div id="comentario" style="display:none">
  <textarea id="Observacao" name="Observacao" style="color: black;"></textarea>
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.