Well, the problem that I have been raised is based on the following:
I have three text fields, in the first two I must enter a word or a short phrase and then in the third I must create a paragraph, later with the join button, the three fields must be joined and the data of the first two fields must be appear at the beginning and at the end of each line of the third text, I leave you a graphic example so that my idea is better understood.
Everything that is written in red I did based on paint. (the blue line is imaginary just to mark space)
In addition to the image, I leave this code that is a similar example, but it is not capable of differentiating the line breaks and then adding the first and second data.
<!DOCTYPE html>
<html>
<head>
<title>Ejemplo de JavaScript</title>
<meta charset="UTF-8">
</head>
<body>
<form name="prueba">
Primera Frase: <input type="text"id="textA"; />
Frase final: <input type="text"id="textB"; />
<br><br>
Contenido Medio
<br>
<textarea name="textC" id="textC" cols="50" rows="10">
</textarea>
<br>
</form>
<script>
function unirTexto(){
var c1=document.getElementById('textA').value;
var c2=document.getElementById('textB').value;
var t1=document.getElementById('textC').value;
var resultado= document.getElementById('resultado');
resultado.innerHTML = c1+t1+c2;
}
function limpiar() {
document.getElementById('textA').value = "";
document.getElementById('textB').value = "";
document.getElementById('textC').value = "";
resultado.innerHTML = " ";
}
</script>
<button onclick="unirTexto()" id="Boton">Unir</button>
<button onclick="limpiar()">Limpiar</button> <br>
Resultado: <span id="resultado"></span>
<br>
</body>
</html>
You could take take the value of
textarea
and then separate them with regular expressions by the line breaks witht1.split(/\n/g)
and then iterate through it in afor ()
, for example: