click
event
onclick
,
onload
,
onkeypress
, etc.
<input type="button" value="Click Me" onclick="alert('You clicked the button!')">
click
property
<input type="button" value="Click Me" id="mybutton">
<script>
var button = document.getElementById("mybutton");
button.onclick = buttonClick;
function buttonClick() {
alert("You clicked the button!");
}
</script>
<input type="button" value="Click Me" id="mybutton">
<script>
var button = document.getElementById("mybutton");
button.addEventListener("click", buttonClick);
function buttonClick() {
alert("You clicked the button!");
}
</script>
document.getElementById(id)
- Important to execute after the DOM has loaded (more later)
obj.addEventListener("event", handler)
- registers
an event handler for a particular event. Event handler must be a zero-argument
function or a function that takes an event
parameter (more later)
addEventListener()
can be used on any element like an image,
a button, or the document
// Register event handler for the entire page
document.addEventListener("click", buttonClick);
buttonClick()
immediately instead of when button is clicked:
// WRONG! Don't put () after event handler!
button.addEventListener("click", buttonClick());
button.addEventListener("click", function() {
alert("You clicked the button!") });
load
and DOMContentLoaded
events
load
- Triggered when the web page has finished loading (including images,
stylesheets, etc.)
DOMContentLoaded
- Triggered when the document has been completely loaded and parsed,
without waiting for images, stylesheets, and subframes to finish loading
DOMContentLoaded
event handler is ideal for registering other event handlers
because all DOM elements can now be found by getElementById()
function start() {
// Register event listeners
var button = document.getElementById("mybutton");
button.addEventListener("click", buttonClick);
}
// Execute start after the DOM is ready
window.addEventListener("DOMContentLoaded", start);
mouseover
and mouseout
events
<script>
// Cache images for quick response
var imageOn = new Image();
imageOn.src = "images/robotman_invert.png";
var imageOff = new Image();
imageOff.src = "images/robotman.png"
function mouseOver(event) {
event.target.setAttribute("src", imageOn.src);
// OR
// document.getElementById("robotman").src = imageOn.src;
}
function mouseOut(event) {
event.target.setAttribute("src", imageOff.src);
// OR
// document.getElementById("robotman").src = imageOff.src;
}
function start() {
var img = document.getElementById("robotman");
img.addEventListener("mouseover", mouseOver);
img.addEventListener("mouseout", mouseOut);
}
addEventListener("DOMContentLoaded", start);
</script>
<img src="images/robotman.png" id="robotman" alt="Robotman">
event
is passed to event handlers which contains extra
information about the event that fired
event.target
- element that received the event
mousemove
- triggered when the mouse is moved
mouseup
and mousedown
- triggered when the mouse button
is pressed and released
keydown
, keypress
, keyup
- triggered when
the user is pressing, presses, and releases a key
focus
and blur
- triggered when focus is given to
or removed from a control
submit
and reset
- triggered when a form's submit or reset
button is pressed