Aral Code

Graphics & Scripts Lesson 35 of 42

HTML and JavaScript — Your First Script

Learn how JavaScript connects to HTML with the script element, change text when a button is clicked, and know where to put your scripts.

HTML gives a page its content. CSS makes it look good. JavaScript makes it do things: react to clicks, check answers, count totals, show and hide parts of the page. This lesson is not a full JavaScript course. It shows you how JavaScript connects to HTML, so you understand what's happening when you see it in other people's pages.

The script element

JavaScript code goes inside a <script> element. When the browser reaches it, it runs the code.

Example Your first script
<p id="greeting">Hello!</p>

<script>
  document.getElementById("greeting").innerHTML = "Magandang araw, mga estudyante! 👋";
</script>

Let's read that line from left to right:

  • document — the whole web page
  • .getElementById("greeting") — find the element whose id is greeting
  • .innerHTML = "..." — replace what's inside it with new content

The paragraph first says "Hello!", but the script changes it before you can even see it. This is why id attributes are so important: they let JavaScript find a specific element.

Reacting to a click

Scripts become useful when they respond to what the user does. The simplest way is the onclick attribute on a button.

Example Click to change text
<p id="msg">Click the button.</p>
<button type="button" onclick="document.getElementById('msg').innerHTML = 'Salamat sa pag-click! 😊'">
  Click me
</button>

Notice the quotes. The onclick value is wrapped in double quotes "...", so the text inside the JavaScript uses single quotes '...'. If you used double quotes for both, the browser would think the attribute ends early.

Changing styles

JavaScript can change CSS too, through the style property:

Example Light switch
<div id="room" style="padding: 30px; background: #fff8dc; text-align: center;">
  💡 The light is ON
</div>
<button type="button" onclick="
  document.getElementById('room').style.background = '#222';
  document.getElementById('room').style.color = 'white';
  document.getElementById('room').innerHTML = '🌙 The light is OFF';
">Turn off</button>

Using functions

Writing long code inside onclick gets messy fast. Instead, put the code in a function inside a <script>, give it a name, then call that name from the button.

Example A tip calculator for a carinderia
<label>Total bill (₱): <input id="bill" type="number" value="350"></label>
<p>
  <button type="button" onclick="computeTip(10)">10% tip</button>
  <button type="button" onclick="computeTip(15)">15% tip</button>
</p>
<p id="result"></p>

<script>
  function computeTip(percent) {
    const bill = Number(document.getElementById("bill").value);
    const tip = bill * percent / 100;
    document.getElementById("result").innerHTML =
      "Tip: ₱" + tip.toFixed(2) + " — Total: ₱" + (bill + tip).toFixed(2);
  }
</script>

The function computeTip takes a number (percent), reads the bill from the input box, computes the tip, and writes the answer into the result paragraph. Both buttons reuse the same function with different numbers. Change the bill amount and click again.

Where to put the script element

A script can only find elements that already exist on the page. The browser reads your HTML from top to bottom, so if a script runs before the element it's looking for, it finds nothing.

The two common, safe choices:

  1. At the end of the body, just before </body>. By then, every element above it already exists.
  2. In the head with defer, using an external file: <script src="app.js" defer></script>. The defer attribute tells the browser to wait until the whole page is read before running it.

External JavaScript files

Just like CSS, JavaScript can live in its own file, usually ending in .js. This keeps your HTML clean and lets many pages share the same script.

<!-- index.html -->
<script src="script.js" defer></script>
// script.js
document.getElementById("year").innerHTML = new Date().getFullYear();

Note that an external script uses src and the <script> element stays empty. Code inside the tags is ignored when src is present.

The noscript element

Some people turn JavaScript off, and some old devices don't support it well. <noscript> shows a message only to those visitors:

<noscript>
  <p>This quiz needs JavaScript. Please turn it on in your browser settings.</p>
</noscript>

The browser console

When a script doesn't work, the browser usually tells you why in the console. On a computer, press F12 (or Ctrl + Shift + J) and click the Console tab. Red text means an error, and it usually includes the line number. You can also print your own messages there with console.log("checking..."), which is the first debugging tool every programmer learns.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which element holds JavaScript code in an HTML page?
2. What does document.getElementById("total") do?
3. A script in the head (no defer) tries to change a paragraph lower on the page and fails. Why?
4. How do you link an external file called app.js?

By Sir Reyel · Last updated 2026-12-04