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>