How to do when to click "send" change the final text Form

0

I want the result of the content typed in the "Text" field to automatically exit from a predefined text within the "Post Text" field. Home Ex: when typing exemplo in the "Text" field and clicking send, the result will automatically appear in "Post Text" Seu "exemplo" aqui or <<<< exemplo >>>> <div>exemplo</div>

In moments like this:

WheredoIwanttogo?:

<script>
  function submitted() {
    formValue = document.getElementsByName("texto")[0].value;
    document.getElementsByName("texto")[1].setAttribute("value", formValue); // Copia
    return false;
  }
</script>

<form onsubmit="return submitted()"> 
  Texto:<br><input type="text" name="texto">
  <input type="submit" value="Enviar"><br><br> 
  Pós texto:<br><input type="text" name="texto">
</form>
    
asked by anonymous 23.06.2018 / 19:14

2 answers

2

You can already get the value of the first input , then just concatenate what you want using the + sign (similar to . in PHP).

But you can use other syntax and exclude the use of setAttribute :

document.getElementsByName("texto")[1].value = 'seu "'+formValue+'" aqui';

Example:

function submitted() {
   formValue = document.getElementsByName("texto")[0].value;
   document.getElementsByName("texto")[1].value = 'seu "'+formValue+'" aqui';
   return false;
}
<form onsubmit="return submitted()"> 
  Texto:<br><input type="text" name="texto">
  <input type="submit" value="Enviar"><br><br> 
  Pós texto:<br><input type="text" name="texto">
</form>

Adding values from multiple fields:

function submitted() {
   var formValue0 = '<div>'+ document.getElementsByName("texto")[0].value +'</div>';
   var formValue1 = '<script>'+ document.getElementsByName("texto")[1].value +'<\/script>';
   var formValue2 = '<style>'+ document.getElementsByName("texto")[2].value +'</style>';
   document.getElementsByName("texto")[3].value = formValue0+formValue1+formValue2;
   return false;
}
<form onsubmit="return submitted()"> 
  Campo 1:<br><input type="text" name="texto"><br>
  Campo 2:<br><input type="text" name="texto"><br>
  Campo 3:<br><input type="text" name="texto"><br>
  <input type="submit" value="Enviar"><br><br> 
  Pós texto:<br><input type="text" name="texto">
</form>

To concatenate the </script> tag inside the code, you need to escape the closing bar, otherwise the code will interpret as if you were closing the script:

 ↓
<\/script>
    
23.06.2018 / 21:15
0

I will post my comment as a reply, already tailoring to com outros campos etc ..

  

Just make use of concatenation.

Concatenation We can understand concatenation as the joining of two strings (phrases or words). The operator to do concatenation is the plus sign + .

function submitted() {
   var formValue = '<div>'+ document.getElementsByName("texto")[0].value +'</div>';
   formValue += '<script>'+ document.getElementsByName("texto")[1].value +'<\/script>';
   formValue += '<style>'+ document.getElementsByName("texto")[2].value +'</style>';
   document.getElementsByName("texto")[3].value = formValue;
   return false;
}
<form onsubmit="return submitted()"> 
  Campo 1:<br><input type="text" name="texto"><br>
  Campo 2:<br><input type="text" name="texto"><br>
  Campo 3:<br><input type="text" name="texto"><br>
  <input type="submit" value="Enviar"><br><br> 
  Pós texto:<br><input type="text" name="texto">
</form>

For textarea , for each to be on a separate line, and not to send blank or null fields.

linha separada just add \n (new line) to the location you want to break the line.

para não enviar campos em branco use method trim()

  

The trim () method removes the unnecessary spaces declared at the beginning and / or end of a string.

Browser Compatibility

function submitted() { 
   
var texto0= (document.getElementsByName("texto")[0].value).trim();
 var texto1= (document.getElementsByName("texto")[1].value).trim();
  var texto2= (document.getElementsByName("texto")[2].value).trim();
   
   if(texto0!=""){
    texto0= '<div>'+ texto0 +'</div>\n';
   }
   if(texto1!=""){
   texto1= '<script>'+ texto1 +'<\/script>\n';
   }
   if(texto2!=""){
   texto2= '<span>'+ texto2 +'<\/span>\n';
   }

   var formValue = texto0 + texto1 + texto2;
   document.getElementsByName("texto")[3].value = formValue;
   return false;
}
<form onsubmit="return submitted()"> 
  Campo 1:<br><input type="text" name="texto"><br>
  Campo 2:<br><input type="text" name="texto"><br>
  Campo 3:<br><input type="text" name="texto"><br>
  <input type="submit" value="Enviar"><br><br> 
  <textarea name="texto" style="height:200px"></textarea
</form>
  

In closing tags </script> you should escape the / bar with a backslash \ , that is <\/script>

     

can also use as follows: </scr'+'ipt>

formValue += '<script>'+ document.getElementsByName("texto")[1].value +'</scr'+'ipt>\n';
  

When it splits into two strings and concatenates, it will not disturb the rendering engine

    
23.06.2018 / 23:33