Содержание
- How to Manage Text Input and Output with JavaScript for HTML5 and CSS3 Programming
- Introducing event-driven programming
- Creating the HTML form
- Text Box Input and Output
- Use getElementById to get access to the page
- Manipulating the text fields
- About This Article
- This article is from the book:
- About the book author:
How to Manage Text Input and Output with JavaScript for HTML5 and CSS3 Programming
Perhaps the most intriguing application of the DOM in JavaScript is the ability to let the user communicate with the program through the HTML5 web page, without all those annoying dialog boxes. Here is a page with a web form containing two textboxes and a button.
When you click the button, something exciting happens.
Clearly, form-based input and output is preferable to the constant interruption of dialog boxes.
Introducing event-driven programming
Graphic user interfaces usually use a technique called event-driven programming. The idea is simple.
- Create a user interface. In web pages, the user interface is usually built of HTML and CSS.
- Identify events the program should respond to. If you have a button, users will click it. (If you want to guarantee they click it, put the text “Launch the Missiles” on the button.) Buttons almost always have events. Some other elements do, too.
- Write a function to respond to each event. For each event you want to test, write a function that does whatever needs to happen.
- Get information from form elements. Now you’re accessing the contents of form elements to get information from the user. You need a mechanism for getting information from a text field and other form elements.
- Use form elements for output.
Creating the HTML form
The first step in building a program that can manage text input and output is to create the HTML framework. Here’s the HTML code:
Text Box Input and Output