On this page10 sections
  1. Learning Objectives
  2. 1. The <script> Element
  3. 2. What JavaScript Can Change
  4. 3. Events
  5. 4. External JavaScript Files
  6. 5. <noscript>
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 020: JavaScript in HTML

Category4: Page Structure
Lecture020 of 048
Estimated video length12 min
TargetHTML5 · any modern browser
LevelBeginner

HTML builds the page and CSS styles it. JavaScript makes it do things: react to clicks, change text, check a form, load new data. This lecture shows how JavaScript plugs into HTML. Learning the language itself is a course of its own.


Learning Objectives

  • Add JavaScript with the <script> element, inline or from a file
  • Change content, styles and attributes from JavaScript
  • React to events such as onclick
  • Show a message with <noscript> when JavaScript is turned off

1. The <script> Element

Example

html
<p id="demo">The script will replace this text.</p>

<script>
  document.getElementById("demo").innerHTML = "Hello from JavaScript!";
</script>

document.getElementById("demo") finds the element; .innerHTML changes what's inside it.


2. What JavaScript Can Change

Content

html
<p id="msg">Old text</p>
<button type="button" onclick="document.getElementById('msg').textContent = 'New text!'">Change text</button>

Styles

html
<p id="box">Watch me change colour.</p>
<button type="button" onclick="document.getElementById('box').style.color = 'crimson'">Make it red</button>
<button type="button" onclick="document.getElementById('box').style.fontSize = '28px'">Make it big</button>

Attributes

html
<img id="pic" src="https://picsum.photos/id/1025/240/160" alt="A dog" width="240" height="160">
<br>
<button type="button" onclick="document.getElementById('pic').src = 'https://picsum.photos/id/1074/240/160'">Show another photo</button>

3. Events

An event is something that happens: a click, a key press, the page finishing loading. onclick, oninput and onchange are event attributes.

Example: the text updates as you type:

html
<input id="name" placeholder="Type your name" oninput="document.getElementById('hello').textContent = 'Hello, ' + this.value">
<p id="hello">Hello,</p>

On bigger projects, attach events in JavaScript instead of attributes:

html
<button type="button" id="btn">Click</button>
<script>
  document.getElementById("btn").addEventListener("click", () => alert("Clicked!"));
</script>

4. External JavaScript Files

Keep scripts in a .js file and link it, like a stylesheet:

html
<script src="app.js" defer></script>
  • Put it in the <head> with defer: it downloads early and runs after the page is built.
  • An external script tag stays empty, with no code between the tags.

5. <noscript>

Shown only when JavaScript is turned off or blocked:

html
<noscript>This page needs JavaScript to show the timetable.</noscript>

Common Beginner Mistakes

1. Running a script before the element exists. A script in the <head> (without defer) can't find elements further down. Use defer or put the script at the end of <body>.

2. Wrong id. getElementById("Demo") won't find id="demo". Ids are case-sensitive.

3. Code inside an external <script src> tag. It's ignored. Use two separate <script> tags.

4. Quote clashes in onclick. Use double quotes for the attribute and single quotes inside, as in the examples.


Practice Questions

  1. Which element adds JavaScript to a page?
  2. Write JavaScript that changes the text of id="title" to "Welcome".
  3. What does defer do on a script tag?
  4. Name two event attributes.
  5. When is <noscript> content shown?
  6. Write a button that turns the page background light blue.

Practice Answers

  1. <script>
  2. document.getElementById("title").textContent = "Welcome";
  3. It downloads the script straight away but runs it only after the page has been built.
  4. Any two of onclick, oninput, onchange, onsubmit, onload.
  5. When JavaScript is turned off or blocked.
  6. <button type="button" onclick="document.body.style.background = 'lightblue'">Blue</button>

Key Takeaways

  • <script> holds JavaScript, inline or via src="file.js" with defer.
  • getElementById finds an element; then change .textContent, .style or attributes.
  • Events (onclick, oninput…) run code when something happens.
  • <noscript> covers visitors without JavaScript.