DOM - textarea 2 text
/Html JavaSript/DOM - textarea 2 text.html
<script>
// --------------------------------------------------------------------------
// Version SIMPLE (si pas de html à ajouter) :
// --------------------------------------------------------------------------
/* // marche pas sous IE...
function SetTxt(idNode, txt){
document.getElementById(idNode).childNodes[0].nodeValue=txt;
} */
function SetTxt(idNode, txt){
var child, div = document.getElementById(idNode);
while(child = div.firstChild) div.removeChild(child);
div.appendChild(document.createTextNode(txt));
}
function CopyTo(idFrom, idNode){
document.getElementById(idNode).childNodes[0].nodeValue=document.getElementById(idFrom).value;
}
// --------------------------------------------------------------------------
// Version Complette (ajout de <br> en fin de ligne) :
// --------------------------------------------------------------------------
function Textarea2Div(idTextareaSource, idDivDest){
var lignes = document.getElementById(idTextareaSource).value.split('\n');
var ll, child, div = document.getElementById(idDivDest)
while(child = div.firstChild) div.removeChild(child);
if(lignes.length==1 && lignes[0]=='') return;
for(ll in lignes){
var txt = document.createTextNode(lignes[ll]);
div.appendChild(txt);
var BR = document.createElement('br');
div.appendChild(BR);
}
}
</script>
<textarea id="TxtFOO" cols="30" rows="5">
ligne 1
ligne 2
ligne 3
</textarea>
<input type="button" value="maj" onClick="Textarea2Div('TxtFOO', 'divFOO')">
<div id="divFOO">
ici le div à màj
</div>