Uses the "onchange" event handler: JavaScript is activated after the field is deselected, that is, when you move the focus from the input field to somewhere else.
Here's the code:
<form action="javascript:;" name="form1" method="post"> Enter some text here:<br /> <input type="text" length="30" name="textInput" maxlength="25" onchange="window.alert('This text string is ' + textInput.value.length + ' characters long.'); textInput.value=''; /* resets the input field value */ return true;" /> /* signals the browser that the execution of the block has ended */ </form>
Note:
window.alert()
and textInput.value.length
is an access operator, meaning "something of...". window.alert()
calls the alert method of the window object.textInput.value.length
asks for the length of the value of the input field named textInput. Note that the form field is addressed by its name, "textInput".