---
description: With the DOM, Python can access and change all the elements of window.
---

# DOM

DOM methods are **actions** you can perform (on Elements).

DOM properties are **values** (of Elements) that you can set or change.

## The DOM Programming Interface

The DOM can be accessed with Python.

In the DOM, all elements are defined as **objects**.

The programming interface is the properties and methods of each object.

A **property** is a value that you can get or set (like changing the content of an element).

A **method** is an action you can do (like add or deleting an element).

#### Example

```python
document.getElementById("demo").innerHTML = "Hello World!"
```

In the example above, `getElementById` is a **method**, while `innerHTML` is a **property**.

### The getElementById Method

The most common way to access an element is to use the `id` of the element.

In the example above the `getElementById` method used `id="demo"` to find the element.

### The innerHTML Property

The easiest way to get the content of an element is by using the `innerHTML` property.

The `innerHTML` property is useful for getting or replacing the content of elements.

The `innerHTML` property can be used to get or change any element.

## Document

The HTML DOM document object is the owner of all other objects in your UI.

### The HTML DOM Document Object

The document object represents your UI.

If you want to access any element, you always start with accessing the document object.

Below are some examples of how you can use the document object to access and manipulate HTML.

### Finding HTML Elements

| 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   |

### Changing HTML Elements

| Property                                        | Description                                   |
| ----------------------------------------------- | --------------------------------------------- |
| _element_.innerHTML =  _new html content_       | Change the inner HTML of an element           |
| _element_._attribute = new value_               | Change the attribute value of an HTML element |
| _element_.style._property = new style_          | Change the style of an HTML element           |
| Method                                          | Description                                   |
| _element_.setAttribut&#x65;_(attribute, value)_ | Change the attribute value of an HTML element |

### Adding and Deleting Elements

| 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(_new, old_) | Replace an HTML element           |
| document.write(_text_)            | Write into the HTML output stream |

### Adding Events Handlers

| Method                                           | Description                                                                   |
| ------------------------------------------------ | ----------------------------------------------------------------------------- |
| document.getElementById(_id_).onclick = function | Adding event handler code to an onclick event :warning: **Not supported yet** |

{% hint style="danger" %}
Above method is Not supported yet. Instead use

document.getElementById(_id_).addEventHandler(event, handler)
{% endhint %}
