TExcelTAD
Home › HTML › HTML JavaScript

HTML JavaScript

HTML describes what is on a page; JavaScript makes it do things. With a few lines of script you can react to clicks, change text and styles, validate forms and fetch data. This lesson shows how HTML and JavaScript connect — the JavaScript course covers the language itself.

The script element

JavaScript is added with the <script> element. Code can live inside the tag or in an external file:

html
<!-- inline script -->
<script>
  console.log("Hello from the page");
</script>

<!-- external script -->
<script src="app.js"></script>
Attribute Meaning
src URL of an external .js file (the tag must then be empty)
defer Download in parallel, run after the HTML is parsed, in order
async Download in parallel, run as soon as ready (order not guaranteed)
type="module" Treat the file as an ES module (import/export, deferred by default)

Tip: Either put plain scripts at the end of <body>, or put them in <head> with defer. Both make sure the HTML elements exist before your code tries to use them.

Changing content

The most common job is to find an element and change it. document.getElementById finds an element by id; textContent replaces its text.

html · try it yourself
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
  <h2 id="title">Welcome!</h2>
  <button onclick="document.getElementById('title').textContent = 'You pressed the button 🎉'">
    Change the heading
  </button>
</body>
</html>

The onclick attribute above is an inline event handler. It is quick for demos, but in real projects it is cleaner to keep JavaScript out of the HTML and attach handlers with addEventListener:

html · try it yourself
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
  <p id="msg">The light is off.</p>
  <div id="bulb" style="width:60px;height:60px;border-radius:50%;background:#555"></div>
  <button id="toggle">Toggle light</button>

  <script>
    const bulb = document.getElementById("bulb");
    const msg = document.getElementById("msg");
    let on = false;

    document.getElementById("toggle").addEventListener("click", () => {
      on = !on;
      bulb.style.background = on ? "gold" : "#555";
      bulb.style.boxShadow = on ? "0 0 30px gold" : "none";
      msg.textContent = on ? "The light is on." : "The light is off.";
    });
  </script>
</body>
</html>

Here the script changes styles (element.style.background) as well as text.

Reacting to input

Events fire for many things: click, input (typing), change, submit, mouseover, keydown and more. This example updates a live preview as you type.

html · try it yourself
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
  <label for="name">Your name:</label>
  <input id="name" placeholder="Type here">
  <p>Hello, <strong id="out">stranger</strong>!</p>

  <script>
    const input = document.getElementById("name");
    const out = document.getElementById("out");
    input.addEventListener("input", () => {
      out.textContent = input.value.trim() || "stranger";
    });
  </script>
</body>
</html>

Note: Use textContent for text supplied by users. innerHTML interprets the string as HTML, so a user could inject tags or scripts into your page.

Creating elements

Scripts can also build new HTML elements and add them to the page.

html · try it yourself
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
  <ul id="list"></ul>
  <script>
    const fruits = ["Apple", "Banana", "Cherry"];
    const list = document.getElementById("list");
    for (const f of fruits) {
      const li = document.createElement("li");
      li.textContent = f;
      list.appendChild(li);
    }
  </script>
</body>
</html>

The noscript element

<noscript> holds content shown only when JavaScript is disabled or unsupported. Use it to explain that a feature needs scripting.

html
<noscript>
  <p>This calculator needs JavaScript. Please enable it in your browser.</p>
</noscript>

Useful DOM methods

Method / property What it does
document.getElementById("x") Element with id="x"
document.querySelector(".card") First element matching a CSS selector
document.querySelectorAll("li") All matching elements
el.textContent Read/write the text
el.style.color Read/write an inline style
el.classList.add("on") Add a class (also remove, toggle)
el.addEventListener("click", fn) Run fn when the event happens
document.createElement("p") Make a new element

Test yourself

Which element embeds JavaScript in an HTML page?

  1. <js>
  2. <javascript>
  3. <script>
  4. <code>

What does the defer attribute do on an external script?

  1. Never runs the script
  2. Runs it after the HTML has been parsed
  3. Runs it before any HTML is read
  4. Runs it only on mobile

When is <noscript> content displayed?

  1. Always
  2. When JavaScript is disabled or unavailable
  3. When a script has an error
  4. Only in old browsers