Keith

Document Object Model Sandbox

by Keith Rowles • 27/10/2023JavaScript

Hour glass placed in some sand

Summary

Playing around with html document object model (dom) and changing elements using javascript.

There are so many to demonstrate I will just list a few to demonstrate my understanding.

Lets add a couple of fiddles.

classList

addEventListener


Next, I will add some dynamic astro components into the page.

The astro component is a .mdx file located in the components directory which I import into the mdx blog post file.

import DomSandbox from '../../components/mdx/DomSandbox.astro';

The appendChild() Method

  • Coffee
  • Tea

Click "Append" to append an item to the end of the list:


DOM Attributes

Landscape

The attributes of the image are:

Output will go here...

Click "try it" to display the numer of button attributes:


The getAttribute() Method

Learn more about Link To My Website


Get Element By Tag Name

A h3 element in div.

P element in div.

A span element in div.

A h2 element in div.

A div element in div.

Another p element in div.


Get Elements By Class Name

First p element with class="child" in a div (index 0).

Second p element with class="child" in a div (index 1).

Third p element with class="child" in a div (index 2).

Click the button to add a background color to all the elements with class="child" inside the div element.


Inner HTML

Click the button to change the HTML content, attributes and values of the a element.

My Data Analyst Website


insertAdjacentHTML() Method

A Heading on the page

Click the button to insert a paragraph after the heading and before the heading:


innerText

I am a paragraph.

Click the button get the inner text of the p element above.


outerHTML

This heading will change!

Click the button to change the header from a h1 to a h3.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Quam maxime quasi ea dolorum. Et aliquid quo nulla omnis, unde inventore corrupti impedit explicabo voluptatibus, obcaecati suscipit architecto. Nam, aspernatur cum.


Query Selector All

A heading with class="example" in div

A p element with class="example" in a div.

Another p element with class="example" in a div.

A p element in div with a span element with class="example" inside of it.