+ 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); }
4 ответов
+ 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
+ 1
https://code.sololearn.com/W211Pe19J10Y/?ref=app
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>
0
Could you please tell us what is the purpose of putting the ("p") in the above code line???