Tip - Hooking Events

Suppose you have a div called 'myDiv':

<!-- In your HTML -->
<div id='myDiv'>...</div>

You can hook a handler function called doSomething to it using Event.observe like so:

// Somewhere in your JavaScript page init code
Event.observe('myDiv', 'click', doSomething);


// Somewhere in your JavaScript page init code
$('myDiv').observe('click', doSomething);

That's just one way, there are others that don't require that the div have an ID.

For more details, check out the Event Hooking "How To".

For more events see http://www.w3schools.com/jsref/jsref_events.asp - Remember to remove the prepended 'on' when coding

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License