how should a textarea be cleaned properly?

0

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";
    n=n+id.toString();
    let imagen=document.getElementById(id);
    let cajaComentarios=document.createElement("textarea");
    cajaComentarios.setAttribute("id",n);
    cajaComentarios.setAttribute("name","texto");
    cajaComentarios.setAttribute("rows","1");
    cajaComentarios.setAttribute("cols","45");
    cajaComentarios.setAttribute("onkeypress","insertarComentario(event,this.parentNode.id)");
    imagen.appendChild(cajaComentarios);
  }


      function insertarComentario(e,id){

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

    if(e.keyCode == 13){


      var objeto= new XMLHttpRequest();

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

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

    comentario.appendChild(texto);
    padre.parentNode.appendChild(comentario);
    padre.children[0].value="";

      }

    }

  }
    
asked by Leo 17.12.2018 в 02:15
source

1 answer

0

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="https://codepen.io"> CodePen and share the link.

JavaScript:

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