Java Reference
In-Depth Information
The
blur
event occurs when the user moves the focus away from the form element. Add
the following to scripts.js, reload the page, and then move the cursor away from the search
box:
input.addEventListener('blur', function(){
alert("blurred")},
↵
false);
The
change
event occurs when the user moves the focus away from the form element
after changing it
. So if a user clicks in an input field and makes no changes, and then clicks
elsewhere, the
change
event won't fire but the
blur
event will.
Add the following code to scripts.js and reload the page. You'll notice that the alert mes-
sage
"changed"
only appears if you actually change the value inside the search box, then
move the cursor away from it:
input.addEventListener('change', function(){
alert("changed")},
↵
false);
Note that the
blur
event will also fire, but after the
change
event.
Submitting a Form
Possibly the most important form event is the
submit
event, occurring when the form
is submitted. Usually this will send the content of the form to the server to be processed,
but we can use JavaScript to intercept the form before it's sent by adding a
submit
event
listener. Add the following code to the scripts.js file:
var form = document.forms.search;
form.addEventListener ("submit", search, false);
function search() {
alert("Submitted");
}
