Assignment 016

Functions

Functions are special, reusable chunks of code that save you lots of time. They are normally coded in the head section of a Web page and called by other objects when needed, through events. In this example, all the buttons below call the callMe() function when they are clicked. Therefore, the button's onClick events call the function callMe(). When the buttons are clicked, they send their name to the function. The function handles the window.alert.


Here's the code used to create the function (place this code in the section of your page):

<script language="Javascript" type="text/javascript">

function callMe(buttonnumber)
{
window.alert('Hello! You have pressed ' + buttonnumber);
}
</script>

Here's the code used to create the first two buttons (place this code in the section of your page):

<input type="button" name="btnOne" id="btnOne" value="Button 1" onClick="callMe('button1')">
<input type="button" name="btnTwo" id="btnTwo" value="Button 2" onClick="callMe('button2')">

Look at the source code to learn how to do the rest.