I want the text entered in the input to remain inside a created div and go to the 'box' div, but it doesn't want to work for me, what do I do?
<script>
function crear() {
var texto = document.getElementById("text").value;
var node = document.createElement("div");
node.appendChild(texto);
document.getElementById("caja").appendChild(node);
}
</script>
<input type="text"/>
<button onclick="crear()">Crear caja</button>
<div id="caja"></div>
Set the id "text" to your
input
so you can easily get its value :and uses
node.innerHTML = texto
; to assign the value todiv
.If you want to add another element for example an h1 when you generate the div, use appenChild() for this, example:
I see 2 problems.
The error is that your input does not have an id: