# Menambah atau Menghapus Elemen HTML  Dengan DOM Javascript

menambahkan atau menghapus elemen HTML dengen DOM javascript

<table><thead><tr><th width="221.2767333984375">Property/Method</th><th>Penjelasan</th></tr></thead><tbody><tr><td>document.createElement(element)</td><td>Membuat elemen HTML baru</td></tr><tr><td>document.removeChild(element)</td><td>Menghapus elemen HTML</td></tr><tr><td>document.appendChild(element)</td><td>Menambahkan elemen HTML</td></tr><tr><td>document.replaceChild(kata baru, kata lama)</td><td>Mengubah sebuah kata dalam satu elemen</td></tr><tr><td>document.write(text)</td><td>Menuliskan teks pada dokumen HTML</td></tr></tbody></table>

kode : membuat elemen

```html
<!DOCTYPE html>
<html>
<body>

<h1>The Document Object</h1>
<h2>The createElement() Method</h2>

<p>Create a p element with some text:</p>

<script>
// Create element:
const para = document.createElement("p");
para.innerText = "This is a paragraph.";

// Append to body:
document.body.appendChild(para);
</script>

</body>
</html>
```

kode : menghapus elemen

{% code overflow="wrap" %}

```html
<!DOCTYPE html>
<html>
<body>
<h1>The Element Object</h1>
<h2>The removeChild() Method</h2>

<p>Click "Remove" to remove the first item from the list:</p>
<button onclick="myFunction()">Remove</button>

<ul id="myList">
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
</ul>

<script>
function myFunction() {
  const list = document.getElementById("myList");
  list.removeChild(list.firstElementChild);
}
</script>

</body>
</html>

```

{% endcode %}

kode : menambah elemen

{% code overflow="wrap" %}

```html
<!DOCTYPE html>
<html>
<body>
<h1>The Element Object</h1>
<h2>The appendChild() Method</h2>

<ul id="myList">
  <li>Coffee</li>
  <li>Tea</li>
</ul>

<p>Click "Append" to append an item to the end of the list:</p>

<button onclick="myFunction()">Append</button>

<script>
function myFunction() {

// Create an "li" node:
const node = document.createElement("li");

// Create a text node:
const textnode = document.createTextNode("Water");

// Append the text node to the "li" node:
node.appendChild(textnode);

// Append the "li" node to the list:
document.getElementById("myList").appendChild(node);
}
</script>

</body>
</html>

```

{% endcode %}
