DOM Creating content

From ProgZoo
Jump to navigation Jump to search

1) Create Content

You can create a div element an set the content.

We use the document method createElement and the DOM node method appendChild

Input


Output

let e = document.createElement('div');
e.innerHTML = 'Hello';
document.body.appendChild(e);
let e = document.createElement('div');
e.innerHTML = 'Hello world';
document.body.appendChild(e);

2) Create img

You can create a img element an set the src.

We use the document method createElement and the DOM node method appendChild and the node property innerHTML

Input


Output

let a = document.createElement('img');
a.src = '/flags/fr.png';
document.body.appendChild(a);
let a = document.createElement('img');
a.src = '/flags/fr.png';
document.body.appendChild(a);
let e = document.createElement('div');
e.innerHTML = 'France';
document.body.appendChild(e);
Served by: dill at 2025-06-19T02:30

Navigation menu