Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to embed JavaScript in HTML

In this blog post, we're going to explore the world of embedding JavaScript in HTML. If you're just starting out in programming, this is an exciting opportunity to learn about how the two languages work together to create dynamic and interactive web pages. We'll dive into the basics, explore some code examples, and provide helpful analogies along the way.

As we go through this post, remember that it is written with beginners in mind. We'll avoid using jargon whenever possible, and if we do use any, we'll make sure to explain it so that you can easily understand and follow along.

What is JavaScript?

Before we dive into embedding JavaScript in HTML, let's first understand what JavaScript is. JavaScript is a programming language that allows you to add interactivity and dynamic content to your web pages. It is primarily used on the client-side (meaning it runs in the user's browser) and is often used in conjunction with HTML and CSS to build modern web applications.

In simpler terms, if HTML is the structure of a web page (like the walls and roof of a house), and CSS is the styling and appearance (like the paint and decorations), then JavaScript is the functionality and interactivity (like the plumbing and electricity).

Embedding JavaScript in HTML

There are two main ways to embed JavaScript in your HTML files: by using the <script> tag and by linking to an external JavaScript file. We'll discuss both methods in detail below.

Method 1: Using the <script> Tag

One way to embed JavaScript in HTML is by using the <script> tag. The <script> tag is an HTML element that allows you to include JavaScript code directly within your HTML documents.

Here's an example of how to use the <script> tag to include a simple JavaScript alert:

<!DOCTYPE html>
<html>
<head>
  <title>My First JavaScript</title>
</head>
<body>

<h1>My First JavaScript</h1>
<button onclick="myFunction()">Click me</button>

<script>
  function myFunction() {
    alert("Hello, World!");
  }
</script>

</body>
</html>

In this example, we have a simple HTML document with a button that says "Click me." When the user clicks the button, the myFunction() JavaScript function is called. The JavaScript code is placed inside the <script> tag, which is located at the bottom of the <body> element.

It is important to note that the <script> tag can be placed in the <head> or <body> of your HTML document. However, it is generally recommended to place your JavaScript code at the end of the <body> element. This ensures that your HTML content loads before the JavaScript, preventing any potential issues with elements not yet being loaded when the JavaScript tries to interact with them.

Method 2: Linking to an External JavaScript File

Another way to embed JavaScript in your HTML is by linking to an external JavaScript file. This is a great option if you have a lot of JavaScript code or want to reuse the same code across multiple HTML pages.

To link to an external JavaScript file, you will still use the <script> tag. However, instead of placing the JavaScript code directly inside the tag, you will use the src attribute to specify the URL of the external file.

Here's an example of how to link to an external JavaScript file:

<!DOCTYPE html>
<html>
<head>
  <title>My First JavaScript</title>
</head>
<body>

<h1>My First JavaScript</h1>
<button onclick="myFunction()">Click me</button>

<script src="myScript.js"></script>

</body>
</html>

In this example, we have the same HTML structure as before, with a button that says "Click me." However, instead of including the JavaScript code directly in the HTML file, we have created a separate file called myScript.js. The contents of myScript.js are as follows:

function myFunction() {
  alert("Hello, World!");
}

The <script> tag in the HTML file now has the src attribute set to "myScript.js", which tells the browser to load the JavaScript code from that file. This method allows you to keep your JavaScript code separate from your HTML, making your code more organized and easier to maintain.

Working with JavaScript and HTML Elements

Now that we know how to embed JavaScript in our HTML files let's explore how JavaScript can interact with the elements on a web page. In the examples below, we'll use the <script> tag for simplicity, but remember that you can also use external JavaScript files as we've discussed earlier.

Accessing HTML Elements with JavaScript

To access and manipulate HTML elements using JavaScript, you can use the document object and its various methods. For instance, you can use the getElementById() method to select an element by its id attribute.

Here's an example of how to change the text of a paragraph element using JavaScript:

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript and HTML Elements</title>
</head>
<body>

<h1>JavaScript and HTML Elements</h1>
<p id="demo">This is a paragraph.</p>
<button onclick="changeText()">Click me</button>

<script>
  function changeText() {
    document.getElementById("demo").innerHTML = "This is a new paragraph!";
  }
</script>

</body>
</html>

In this example, we have a paragraph element with an id attribute set to "demo". When the user clicks the button, the changeText() JavaScript function is called, which changes the text within the paragraph element to "This is a new paragraph!"

Adding Event Listeners

In the examples above, we used the onclick attribute to trigger JavaScript functions when a button was clicked. While this works, it's often better to use event listeners, which allow you to separate your JavaScript code from your HTML markup.

Here's an example of how to use an event listener to trigger the same changeText() function when the button is clicked:

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Event Listeners</title>
</head>
<body>

<h1>JavaScript Event Listeners</h1>
<p id="demo">This is a paragraph.</p>
<button id="changeButton">Click me</button>

<script>
  function changeText() {
    document.getElementById("demo").innerHTML = "This is a new paragraph!";
  }

  document.getElementById("changeButton").addEventListener("click", changeText);
</script>

</body>
</html>

In this example, we have removed the onclick attribute from the button element and added an id attribute instead. We then use the addEventListener() method in our JavaScript code to attach the changeText() function to the button's "click" event.

Conclusion

In this blog post, we've covered the basics of embedding JavaScript in HTML, including using the <script> tag and linking to external JavaScript files. We've also discussed how to interact with HTML elements using JavaScript and how to use event listeners to separate your JavaScript code from your HTML markup.

As you continue learning and exploring the world of web development, it's important to remember that JavaScript, HTML, and CSS are all essential building blocks of any modern web application. By learning how to effectively combine these languages, you'll be well on your way to creating dynamic, interactive, and engaging web pages and applications.

Happy coding!