quiero crear una nueva línea en el elemento "p". También probé la etiqueta br y la etiqueta \n, pero realmente no funcionó. cuando ejecuto el código, la nueva línea aparece como un espacio entre las líneas.
<p id="log"></p> let log = document.getElementById("log"); let string = `|line1\n|line2`; let array1 = string.split("|"); let i = 0; let x = 0; log.addEventListener("click", () => { document.addEventListener("keypress", type); }) function type() { log.textContent += array1[i].charAt(x); x++; if (x === array1[i].length) { x = 0; i++; } if (array1[i] === undefined) { document.removeEventListener("keypress", type); } }Debido a que está tratando de obtener el elemento antes de cargar HTML, la variable de registro es null en su código.
Por favor, prueba este código.
<p id="log>hello</p> window.onload = function init() { const log = document.getElementById("log"); log.innerText = "\n first \n second"; };