how should a textarea be cleaned properly?


I hope that well, as the title says, I have that doubt or more than doubt, that problem because I was a whole day struggling with this problem. I want to press the enter key after having written something clean textarea thing that is doing well but when there is nothing in the textarea and squeeze enter generates me the same div apparently this line does not cover the case if (valueComentario .length> 0) or maybe it does but when you press enter a blank space is generated which I do not believe because here I tell you to leave empty parent.children [0] .value=""; my question basically is whether it should be cleaned like this or is there some javascript function that does a reset to the textarea or my other idea is that if it is empty and the enter key is pressed in some way the delete key is pressed, thanks for your time : 3 PS: the ajax that does not do anything for now the idea is that save the comment in a database is still missing

        function cajaDeComentarios(id){
    let n="a";
    let imagen=document.getElementById(id);
    let cajaComentarios=document.createElement("textarea");

      function insertarComentario(e,id){

    var padre=document.getElementById(id);
    var valueComentario=padre.children[0].value;
    //crear elemento si existe comentario

    if(e.keyCode == 13){

      var objeto= new XMLHttpRequest();

      var usuario='<?php echo $_SESSION['nomUsuario'];?>';
      var informacionDelComentario="usuario="+usuario+"&comentario="+valueComentario;'POST','/guardarComentario.php',true);

    var comentario=document.createElement("div");
    //crear un nodo de texto
    var texto=document.createTextNode(valueComentario); = "80%"; = "5px"; = "black"; = "Patrick Hand"; = "17px";




asked by Leo 17.12.2018 в 02:15

1 answer


Answering your question I share an example of how to empty the value of your input. You can see the documentation of the method event.prevenDefault in MDN . Anyway you have other features, you may need to refactor or debug another section of your code and it would be clearer if you upload all your example to a SandBox or < a href=""> CodePen and share the link.


const textBox = document.getElementById('textBox');
textBox.addEventListener('keypress', eraseTextBoxValue, false);

function eraseTextBoxValue(evt) {
  const charCode = evt.charCode;
  const textBoxValue = textBox.value;
  if(charCode === 13) { textBox('') } ; 
answered by 17.12.2018 в 04:31