On this page10 sections
Lecture 020: JavaScript in HTML
| Category | 4: Page Structure |
| Lecture | 020 of 048 |
| Estimated video length | 12 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
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
<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
<p id="msg">Old text</p>
<button type="button" onclick="document.getElementById('msg').textContent = 'New text!'">Change text</button>Styles
<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
<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:
<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:
<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:
<script src="app.js" defer></script>- Put it in the
<head>withdefer: 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:
<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
- Which element adds JavaScript to a page?
- Write JavaScript that changes the text of
id="title"to "Welcome". - What does
deferdo on a script tag? - Name two event attributes.
- When is
<noscript>content shown? - Write a button that turns the page background light blue.
Practice Answers
<script>document.getElementById("title").textContent = "Welcome";- It downloads the script straight away but runs it only after the page has been built.
- Any two of
onclick,oninput,onchange,onsubmit,onload. - When JavaScript is turned off or blocked.
<button type="button" onclick="document.body.style.background = 'lightblue'">Blue</button>
Key Takeaways
<script>holds JavaScript, inline or viasrc="file.js"withdefer.getElementByIdfinds an element; then change.textContent,.styleor attributes.- Events (
onclick,oninput…) run code when something happens. <noscript>covers visitors without JavaScript.