17-Events
17-Events
CS380
The keyword this
2
function pageLoad() {
$("ok").onclick = okayClick; // bound to okButton
here
}
function okayClick() { // okayClick knows what DOM object
this.innerHTML = "booyah"; // it was called on
}
window.onload = pageLoad; JS
<fieldset>
<label><input type="radio" name="ducks"
value="Huey" /> Huey</label>
<label><input type="radio" name="ducks"
value="Dewey" /> Dewey</label>
<label><input type="radio" name="ducks"
value="Louie" /> Louie</label>
</fieldset> HTML
function processDucks() {
if ($("huey").checked) {
alert("Huey is checked!");
} else if ($("dewey").checked) {
alert("Dewey is checked!");
} else {
alert("Louie is checked!");
}
alert(this.value + " is checked!");
CS380
} JS
More about events
5
function name(event) {
// an event handler function ...
} JS
CS380
Mouse events
9
CS380
Mouse event objects
10
CS380
Mouse event objects
11
property/method description
clientX, clientY coordinates in browser window
screenX, screenY coordinates in screen
offsetX, offsetY coordinates in element
pointerX(),
coordinates in entire web page
pointerY() *
isLeftClick() ** true if left button was pressed
* replaces non-standard properties pageX and pageY
** replaces non-standard properties button and which
CS380
The Event object
12
window.onload = function() {
$("target").observe("mousemove", showCoords);
};
function showCoords(event) {
this.innerHTML =
"pointer: (" + event.pointerX() + ", " +
event.pointerY() + ")\n"
+ "screen : (" + event.screenX + ", " +
event.screenY + ")\n"
+ "client : (" + event.clientX + ", " +
event.clientY + ")";
JS
CS380