Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to use javascript in HTML

Understanding the Basics of JavaScript

When we talk about JavaScript (JS), it’s like we're speaking about the soul of a website. HTML is the skeleton, CSS is the skin, and JavaScript is what makes it move and interact. Think of it like a puppet show. HTML is the stage and the puppets, CSS is the paint and costumes that make the puppets look good, and JavaScript is the puppeteer who makes them move and talk.

Let's start with our first step towards using JavaScript in HTML.

Including JavaScript in HTML

There are two main ways to use JavaScript in HTML: inline and external.

Inline JavaScript

Inline JavaScript is when you write your JavaScript code directly into your HTML file. This might be useful for small snippets of code, but for larger scripts, it can make your HTML file messy and hard to manage. Here's an example of inline JavaScript:

<button onclick="alert('You clicked me!')">Click me</button>

In this example, when you click the button, the JavaScript function alert() runs and shows a little pop-up message that says "You clicked me!"

External JavaScript

External JavaScript is when you write your JavaScript code in a separate file and then link to that file from your HTML. It's like keeping your clothes in a closet instead of scattered around your room. It keeps your code tidy and makes it easier to manage. Here's an example:

First, create a JavaScript file - let's name it script.js:

function greet() {
  alert('Hello, world!');
}

Then, in your HTML file, link to the JavaScript file:

<script src="script.js"></script>
<button onclick="greet()">Greet</button>

When you click the button, it will run the greet() function from the script.js file, showing a pop-up message saying "Hello, world!".

JavaScript Syntax Basics

Learning a new programming language is like learning a new spoken language. Once you understand the alphabet, forming words and sentences becomes easy. In the case of JavaScript, these alphabets are variables, data types, and functions.

Variables

Variables are like boxes where you can store things. You can put something in, take something out, or change what's inside. Here's how you create a variable in JavaScript:

let name = 'Alice';

In this example, name is the variable, and 'Alice' is the value we're storing in it.

Data Types

Data types define the kind of data we can store in our variables. JavaScript has several data types, but the most commonly used ones are:

  • string for text, like 'Hello, world!'
  • number for numbers, like 42 or 3.14
  • boolean for true/false values
  • object for complex data structures

Functions

Functions are like machines. You give them something, they do something with it, and then they give you something back. Here's an example:

function greet(name) {
  return 'Hello, ' + name + '!';
}

In this example, greet is a function that takes an input (name) and returns a greeting message.

Making Your Website Interactive with JavaScript

JavaScript is where the magic happens in web development. It's what lets your users interact with your website. Here's an example of how you can use JavaScript to make a form react to user input:

First, create an HTML form:

<form id="myForm">
  <label for="name">Name:</label><br>
  <input type="text" id="name" name="name"><br>
  <input type="submit" value="Submit">
</form>
<p id="greeting"></p>

Then, in your JavaScript file, write a function to handle the form submission:

document.getElementById('myForm').onsubmit = function(event) {
  event.preventDefault();

  let name = document.getElementById('name').value;
  let greeting = 'Hello, ' + name + '!';

  document.getElementById('greeting').innerText = greeting;
}

In this example, when the user submits the form, the JavaScript code prevents the page from refreshing (which is the default behavior), gets the value of the text input, creates a greeting message, and then displays the greeting on the page.

This is just the tip of the iceberg when it comes to using JavaScript in HTML. There's so much more you can do, from changing the style of elements in real-time to fetching data from a server and displaying it on your website. But for now, this should give you a solid foundation to start exploring on your own. As with any new skill, practice makes perfect. So, get coding, and have fun!