In some cases, where the event handler is to be shared by several elements, we can define a function and assign a call to that function in the inline event attribute of one or more HTML tags.
For example, the following defines a JavaScript function named flip ( ) and associates it as the event handler for the onclick event of an IMG element.
<script language="JavaScript">
function flip ( ) {
... JavaScript statements ...
}
</script>
<img src="sample.gif" onclick="flip ( )">
This method allows the same event handler to be associated with more than one element in the document. This is useful when the same action needs to be done whenever the given event occurs in any of the elements.
Note that because we used a call to a function, the parentheses following the function name are required.