Attempting to use pure JavaScript to find the length of text in a textbox. Why is it that the verbose method of:
var first_name = document.getElementById("firstname");
first_name.addEventListener("change", howLong(first_name));
function howLong(first_name) {
var p = first_name.value.length;
alert("Length of input is " + p);
}
<form>
<input id="firstname" type="text" />
<input type="submit" /> </form>
does not allow the value to be accessed (always zero), while the use of anonymous function
var first_name = document.getElementById("firstname");
first_name.addEventListener("change", function(){
var p = first_name.value.length;
alert("Length of input is "+p);
} );
<form>
<input id="firstname" type="text"/>
<input type="submit"/> </form>
Works correctly?