Access to HTML DOM

Basic code

Consider an html file containing this code

<!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="utf-8" >
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title> My Webpage </title>

    <script src="script.js" defer></script>
</head>

<body> 
    <div class="myElement"></div>
</body>
</html>

Check that this page leads to an empty blank webpage when viewed from the browser.

> Now create a file called script.js (in the same directory than your html file).
> Add the following JavaScript code in this file script.js
"use strict;"
const element = document.querySelector(".myElement");
element.textContent = "Hello World";

Reload the webpage in the browser and check that the sentence "Hello World" is now appearing.

Explanation

Let first check at the JavaScript syntax

"use strict;"
const element = document.querySelector(".myElement");
element.textContent = "Hello World";

Finally, in the HTML file, the line

<script src="script.js" defer></script>

enables to load the JavaScript file. The defer keyword indicates that the script should run once the HTML DOM element is loaded.
As a result, the HTML code is first parsed by the browser, and the DOM tree is built, before the script in script.js is executed.

Loop

Add the following loop in the JavaScript and check the result on the webpage.

for(let k=0; k<20; k++) {
    element.textContent += "Hello World ";
}

Note that it is possible to use JavaScript to automatize tasks such as in using loops to fill and generate content, while this was not possible in pure HTML and CSS description.

Browser console

Let's consider that your JavaScript code contains a syntax error or bug. This error may result in a complete empty screen that can be hard to debug without extra information.

To help developers to debug and follow the execution of JavaScript code, modern browsers propose an embedded console where information and error can appear, similarly to standard console output in other languages.

Modify the first line of your JavaScript code into the following erroneous one

const element = document.querySelectorBUG(".myElement");
pic/debug.jpg

More generally, you can use the command console.log(), which is similar to print statements of other languages.

Consider the following code

const element = document.querySelector(".myElement");
element.textContent = "Hello World";

for(var k=0; k<20; k++) {
  console.log("k=",k);
  element.textContent += "Hello World ";
}

// any variable can be printed in console
console.log(element.textContent);
and observe the values displayed in the console.

HTML template

In the future, you may start with the following valid HTML template for webpage with css and JavaScript. Adapt the title, and names of CSS and JavaScript files to your needs.

<!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="script.js" defer></script>
    <title> My Webpage </title>
</head>

<body> </body>

</html>