Lesson Nine: The Button Object
- The button object allows the user to decide when to execute your JavaScript code.
- Enter this code into your HTML file (histevents.html) under the h1 element to create a button:
<input type="button" id="b1" value="Click the Button to see Event One" onclick="historyevents(1);">
- The input tag identifies the input element.
- The input element can contain various objects that are identified by the type attribute. In this case the type is a button object.
- The id attribute identifies the button object. In this case the button has the id of b1.
- The value attribute contains the text that is to be shown in the button element.
- The onclick attribute calls a function when the button is clicked. In this case the onclick event (an event occurs when the button is clicked) calls your JavaScript historyevents function. Notice also that the parameter of 1 is being passed to your function.
- Substitute this code for the document.write(event1); } code in your JavaScript file (just below this code: if (eventchoice == 1) }).
button = document.getElementById("b1"); button.value = event1; }
The above code is explained in the next lesson.
- Test your program with the new code. Your page should look like this: Sample