Send a textarea when pressing enter with JS?

0

I'm having problems with my application because I have two types of textarea on the same page. One creates the post and the other creates the comment for this post.

The following code submits the post textarea:

document.onkeyup=function(e){
     if(e.which == 13){
        if(document.getElementById('textarea-post').value == ""){
        } else {
            document.enviaPost.submit();
        }
      }
 }

Code form above:

 <form action="posta.php" method="post" name="enviaPost">
    <ul class="form-post" >
       <li>
          <input type="hidden" name="data" value="<?= date("Y/m/d") ?>" />
          <textarea  placeholder="Como você está se sentindo?" name="contpost" id="textarea-post" /></textarea>
      </li>
          <input type="submit" value="Enviar" class="botao-enviar-post" />
  </ul>

What I would like to know is if, if I put another keyup like this pro textarea of the comment, it will cause some conflict. And if you give, I'd like to know how you can do this without generating a conflict.

Second textarea:

<form  action="init/add_coment.php" method="post" name="enviaComent">
  <input type="hidden" value="<?=$post['id']?>" name="id_post" />
  <textarea id="texto_coment" name="comentario" class="comentario-texto" placeholder="Escreva um comentário..."></textarea> 
</form>
    
asked by anonymous 26.02.2017 / 03:17

1 answer

1

Remember to add the JQuery library to your page

<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

ThiscodewillsendtheformsendPost

$('#textarea-post').keydown(function(event){if(event.keyCode==13){this.form.submit();returnfalse;}});

DothesamethingintheformenviaComent

$('#texto_coment').keydown(function(event){if(event.keyCode==13){this.form.submit();returnfalse;}});

link

    
26.02.2017 / 04:29