With the HTML DOM, JavaScript can access and change all the elements of an HTML document.
With the object model, JavaScript gets all the power it needs to create dynamic HTML:
The HTML DOM document object is the owner of all other objects in your web page.
The document object represents your web page.
If you want to access any element in an HTML page, you always start with accessing the document object.
Method | Description |
---|---|
document.getElementById( id) | Find an element by element id |
document.getElementsByTagName( name) | Find elements by tag name |
document.getElementsByClassName( name) | Find elements by class name |
document.querySelector(selectors); | Returns the first element within the document using CSS like selector |
document.querySelectorAll(selectors); | Returns a list of the elements within the document using CSS like selector |
Method | Description |
---|---|
element.innerHTML = new html content | Change the inner HTML of an element |
element.setAttribute (attribute, value) | Change the attribute value of an HTML element |
element.style. property = new style | Change the style of an HTML element |
Method | Description |
---|---|
document.createElement( element) | Create an HTML element |
document.removeChild( element) | Remove an HTML element |
document.appendChild( element) | Add an HTML element |
document.replaceChild( newChild, oldChild) | Replace an HTML element |
document.write( text) | Write into the HTML output stream |
Method | Description |
---|---|
document.getElementById( id).onclick = function(){ code} | Adding event handler code to an onclick event |
document.getElementById( id).addEventListener('click', handler) | Adding event listener |
document.getElementById( id).removeEventListener('click', handler) | Removing event listener by the reference |
The Browser Object Model (BOM) allows JavaScript to "talk to" the browser.
An HTML event can be something the browser does, or something a user does.
Here are some examples of HTML events:
Often, when events happen, you may want to do something.
JavaScript lets you execute code when events are detected.
Event | Description |
---|---|
onchange | An HTML element has been changed |
onclick | The user clicks an HTML element |
onmouseover | The user moves the mouse over an HTML element |
onmouseout | The user moves the mouse away from an HTML element |
onkeydown | The user pushes a keyboard key |
onload | The browser has finished loading the page |
Event handlers can be used to handle, and verify, user input, user actions, and browser actions:
Many different methods can be used to let JavaScript work with events:
A JavaScript can be executed when an event occurs, like when a user clicks on an HTML element.
Examples of HTML events:
element.addEventListener(event, function, useCapture);
Note that you don't use the "on" prefix for the event; use "click" instead of "onclick"
Event propagation is a way of defining the element order when an event occurs. If you have a <p> element inside a <div> element, and the user clicks on the <p> element, which element's "click" event should be handled first?
The removeEventListener() method removes event handlers that have been attached with the addEventListener() method:
element.removeEventListener("mousemove", myFunction);
https://learn.javascript.ru/event-details События в деталях
https://learn.javascript.ru/forms-controls Формы, элементы управления
https://learn.javascript.ru/document Документ и объекты страницы