Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is JavaScript in html

A Peek into JavaScript in HTML

If you've started learning web development, you've probably come across the term JavaScript. But what is JavaScript in HTML, and why is it important? In this blog, I will explain this concept in a simple and intuitive manner, along with some code examples for a better understanding.

HTML and JavaScript: The Dynamic Duo

Imagine building a house. HTML (HyperText Markup Language) is like the bricks and cement that give structure to your house. But a house is more than just walls and a roof, it's the electricity, the plumbing, the interactive aspects that make it functional and livable. JavaScript provides this functionality. It lets your website respond to user interactions, changing content dynamically and making web pages more interactive.

Let's understand this with an analogy. Imagine HTML as a human body's skeleton, providing the basic structure. JavaScript, then, is the nervous system, controlling how the body behaves.

Embedding JavaScript in HTML

So how do you bring JavaScript into your HTML document? The <script> tag. This tag is used to embed or reference JavaScript code within your HTML file.

Here's a simple example:

<!DOCTYPE html>
<html>
<body>

<h1>My first JavaScript code</h1>

<button type="button" onclick="document.getElementById('demo').innerHTML = Date()">The time is?</button>

<p id="demo"></p>

</body>
</html>

In this code, we have a button with an onclick event. When the button is clicked, it triggers a JavaScript function that changes the content of the paragraph with the id "demo". The content changes to the current date and time.

External JavaScript

Besides embedding, JavaScript code can also be placed in external files (with .js extension). This is a great way to keep your HTML clean and uncluttered, especially when you have a lot of JavaScript code.

<!DOCTYPE html>
<html>
<body>

<h1>My first external JavaScript</h1>

<button type="button" onclick="myFunction()">Click me</button>

<p id="demo"></p>

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

</body>
</html>

In this example, the JavaScript code is stored in an external file called myScript.js. The src attribute in the <script> tag is used to link this external JavaScript file to the HTML file.

JavaScript Makes HTML Come Alive

JavaScript can change HTML content and attribute values. This is the key to creating dynamic and interactive web pages. Here's an example:

<!DOCTYPE html>
<html>
<body>

<h1 id="myH">Hello World!</h1>
<p id="myP">Welcome to my webpage!</p>

<button type="button" onclick="changeContent()">Click me to change content!</button>

<script>
function changeContent() {
  document.getElementById("myH").innerHTML = "Greetings, planet!";
  document.getElementById("myP").innerHTML = "You've unlocked dynamic content!";
}
</script>

</body>
</html>

When you click on the button, the JavaScript function changeContent() is invoked. This function changes the content of the heading and the paragraph to something else. This is a basic example of how JavaScript can make your web pages interactive and responsive to user actions.

Conclusion: The Magic Wand of Web Development

If HTML is the stage, JavaScript is the puppeteer pulling the strings, making the characters dance to its tune. With JavaScript, you bring your static HTML to life, making it dynamic, interactive, and user-friendly. It's like the magic wand in your web developer toolkit, casting enchantments that turn your web pages from stone to living, breathing entities.

Remember, learning JavaScript (or any programming language) is like learning to play an instrument. The more you practice, the better you become. So, keep coding, keep experimenting, and enjoy the magical journey of web development. Happy coding!