I am trying to get the text in a text box as the user types in it (jsfiddle playground):
Handling the input
event is a consistent solution: it is supported for textarea
and input
elements in all contemporary browsers and it fires exactly when you need it:
function edValueKeyPress() {
var edValue = document.getElementById("edValue");
var s = edValue.value;
var lblValue = document.getElementById("lblValue");
lblValue.innerText = "The text box contains: " + s;
}
The text box contains:
I'd rewrite this a bit, though:
function showCurrentValue(event)
{
const value = event.target.value;
document.getElementById("lblValue").innerText = value;
}
The text box contains: