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

Asked

Viewed 74 times

1

I want the result of the content typed in the "Text" field to automatically exit between a preset text within the "Post Text" field".
Ex: when typing exemplo in the "Text" field and click submit, the result will automatically appear in "Post Text" Seu "exemplo" aqui or <<<< exemplo >>>> <div>exemplo</div>

In the moment it’s like this:
No momento está assim

My point:
Final

<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>
  • Your question was not very clear, you want to show the result on another page is this?

  • I rephrased the question

  • But if you do not want to send the form, because it is giving a false Return, pq take the information from a form?

  • formValue = "<div>"+(Document.getElementsByName("text")[0].value)+"</div>";

  • Hi friend! Why keeps alternating the marking of a response to another?

  • Hi sorry I didn’t realize I could only accept 1 as the right answer.

Show 1 more comment

2 answers

2


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

But you can use another syntax and dispense with 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 tag </script> inside the code, you need to escape the closing bar, otherwise the code will interpret as if you were terminating the script:

 ↓
<\/script>
  • Yes, just you take the fields by index [1], [2], [3] no document.getElementsByName("texto")[índice].value and change as you wish.

  • Do not use the answer field to ask questions. I updated my answer with other fields.

  • Just do it with </script> or some other too? type </head> </body> etc..

  • Only in the </script>

  • I changed "Pos Texto" to <textarea name="texto" style="height:200px"></textarea> how do I make each one stay in a separate line?

  • formValue0+'\n'+formValue1+'\n'+formValue2;

Show 1 more comment

0

I will publish my comment as a response, already suitable for com outros campos etc...

Just make use of concatenation.

Concatenation We can understand concatenation to be the joining of two strings (phrases or words). The operator for concatenating 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>

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

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

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

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

Compatibility between browsers

inserir a descrição da imagem aqui

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 the closing tags </script> must escape the bar / with a counter \,that is to say <\/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 interfere with the rendering engine

  • If the field is empty it will send the string the same way, how to make it n send anything from the blank fields?

  • @Markvaaz, but what are you sending? This is not clear in your question or in your comment. All fields? Only the Post text?

  • Actually I will not use the form to send something, I just found the form the easiest way (in my opinion) to do what I need, which is a customizable HTML generator, usually I need to do something that will the msm code repeatedly, and with that agr I can speed up a process that would take 10 minutes to 2 minutes, and also simplify the work in a way that even someone who knows nothing can do

  • Thanks for the help

Browser other questions tagged

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