---
# System prepended metadata

title: Working with the DOM and Element References in Marko

---


The Document Object Model (DOM) is a programming interface that represents the structure and content of a web page. It provides a way to interact with the elements on a webpage, manipulate their properties, and handle events. When working with a web framework like Marko, you are working at a higher level of abstraction. However, sometimes you may need to access the underlying DOM elements directly for more control.

## Element References in Marko

In Marko, when you render a component, you can obtain a reference to a specific element in the rendered output using element references. An element reference is a way to "drop down" to a lower level of abstraction and interact directly with the DOM element associated with the reference.

To obtain an element reference in Marko, you can use a special syntax. For example, consider the following Marko template:

```marko
<div/myElementRef>Hello, Marko!</div>
```

In this example, the `/myElementRef` syntax is used to create an element reference named "myElementRef". Once you have obtained the reference, you can use it to manipulate the associated DOM element.

## Working with the DOM APIs

Once you have an element reference in Marko, you can leverage the powerful DOM APIs to interact with the underlying DOM element. Here are some of the most commonly used DOM APIs grouped by functionality:

### Traversing and Querying the DOM

- [parentNode](https://developer.mozilla.org/en-US/docs/Web/API/Node/parentNode): Retrieves the parent node of the specified element.
- [childNodes](https://developer.mozilla.org/en-US/docs/Web/API/Node/childNodes): Retrieves the child nodes of the specified element.
- [querySelector](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector): Retrieves the first element that matches a specified CSS selector.
- [querySelectorAll](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll): Retrieves a list of all elements that match a specified CSS selector.

### Event Handling

- [addEventListener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener): Attaches an event handler function to the specified element, allowing you to respond to events such as clicks, keypresses, and more.
- [removeEventListener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener): Removes an event handler function previously attached to the element.

### Manipulating Attributes

- [setAttribute](https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute): Sets the value of an attribute on the specified element.
- [getAttribute](https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute): Retrieves the value of an attribute from the specified element.
- [removeAttribute](https://developer.mozilla.org/en-US/docs/Web/API/Element/removeAttribute): Removes an attribute from the specified element.

### Manipulating Classes

- [classList](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList): Provides methods to manipulate the class names of an element, allowing you to add, remove, or toggle classes.

### Creating and Removing Elements

- [createElement](https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement): Creates a new element with the specified tag name.
- [appendChild](https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild): Appends a new child node to the specified parent node.
- [removeChild](https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild): Removes a child node from the specified parent node.

### Scrolling and Focus

- [scrollIntoView](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView): Scrolls the specified element into view.
- [focus](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus): Gives focus to the specified element.
- [blur](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/blur): Removes focus from the specified element.

These are just a few examples of the many APIs available in the DOM. You can explore the [MDN Web API reference](https://developer.mozilla.org/en-US/docs/Web/API) to discover more APIs and their functionalities.

> **Note:** For more detailed information and examples on each DOM API, please refer to the corresponding links provided.

By understanding the DOM and utilizing element references in Marko, you can effectively interact with the underlying DOM elements and create dynamic and interactive web applications.

**References:**

- [Document Object Model (DOM) on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model)
- [Element on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Element)