JavaScript Events
HTML events took place on the HTML element things.
When using JavaScript in HTML pages, JavaScript can trigger these events.
HTML Event
HTML event can be browser behavior can also be user behavior.
The following are examples of HTML event:
- HTML page finishes loading
- When HTML input field changes
- HTML button is clicked
Typically, when an event occurs, you can do something.
When an event is triggered JavaScript can execute some code.
HTML elements can be added to an event attribute using JavaScript code to add HTML elements.
apostrophe:
Double quotes:
In the following example, a button element added onclick attribute (plus code):
Examples
try it"
The above example, JavaScript code content id = "demo" element.
In the next example, the code will modify the content of the element itself (using this .innerHTML):
JavaScript code is usually a few lines of code. More common is invoked by the event properties: |
Common HTML event
Here is a list of some common HTML event:
event | description |
---|---|
onchange | HTML element changes |
onclick | A user clicks on an HTML element |
onmouseover | The user moves the mouse on an HTML element |
onmouseout | Users move the mouse from an HTML element |
onkeydown | User presses a key |
onload | The browser has finished loading the page |
More Event list: the JavaScript Reference - HTML DOM event .
JavaScript What can be done?
Events can be used to handle form validation, the user inputs, user behavior and browser actions:
- Event is triggered when the page loads
- Event is triggered when the page Close
- User clicks a button to perform an action
- Verify the legitimacy of user input
- and many more...
You can use a variety of methods to execute JavaScript event code:
- HTML event attributes can execute JavaScript code directly
- HTML event attributes can call the JavaScript function
- You can specify your own event handler for HTML elements
- You can prevent the occurrence of an event.
- and many more...
In the HTML DOM chapter you will learn more about the event and the event handler knowledge. |