Another way to get the desired result:
window.mostrarTexto= function(valor){
var campo = document.getElementById("campo").value;
div.innerHTML = campo;
}
<input id="campo" type="text" onkeyup="mostrarTexto(this.value)"/>
<div id="div" style="display:block"></div>
The difference of this form with that presented by @AlexSander and basically the event that calls the function
onkeyup
- > function is triggered when the user releases a key
oninput
- > function is triggered when the element receives a user input
Note that oninput
and an attribute added in HTML5
which can make it difficult to use in older browsers ...