+ 2

line break in js

Hello, im trying to do a line break using js, but while adding an element it does not work with \n(i think it would work) or <br/>, for example: <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <script> function ex(){ var h=document.getElementById("name"); var elemento = document.createElement("p"); var txt=document.createTextNode("a\nb\nc\nd\ne"); elemento.appendChild(txt); h.appendChild(elemento); }

26th Sep 2017, 3:05 AM
Juan Felipe Usuga Villegas
Juan Felipe Usuga Villegas - avatar
4 odpowiedzi
+ 1
<body> <div id="name"></div> <script> function ex(){ var h=document.getElementById("name"); var elemento = document.createElement("p"); elemento.innerHTML = "a<br>b<br>c<br>d<br>e"; h.appendChild(elemento); } ex() </script> </body> https://code.sololearn.com/Wfc3bsqyI0k4/?ref=app
26th Sep 2017, 4:16 AM
Calviղ
Calviղ - avatar
+ 1
https://code.sololearn.com/W211Pe19J10Y/?ref=app
26th Sep 2017, 5:12 AM
Calviղ
Calviղ - avatar
0
but if i want to add another element, innerHTML Will delete the last Word... for example: <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <div id="name"></div> <script> function ex(){ var h=document.getElementById("name"); var elemento = document.createElement("p"); elemento.innerHTML = "a<br>b<br>c<br>d<br>e"; h.appendChild(elemento); elemento.innerHTML="<br>hola"; h.appendChild(elemento); } ex() </script> </body> </html>
26th Sep 2017, 5:05 AM
Juan Felipe Usuga Villegas
Juan Felipe Usuga Villegas - avatar
0
Could you please tell us what is the purpose of putting the ("p") in the above code line???
29th Sep 2017, 4:33 PM
Lo Sheung Yan
Lo Sheung Yan - avatar