How to add JavaScript to HTML?

In some of my previous posts started appearing JavaScript, so why today I would like to explain more how to nest the code into HTML. You will learn more about JavaSript in later chapters, but today in a short introduction to post you need to know what JavaScript is and what JavaScript do. JavaScript is the programming language for the web and can update and change both HTML and CSS.

JavaScript Introduction

You can place any number of scripts in your HTML document. You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML, in the <body> section, or after the </body> close tag, depending on when you want Java Script to load. 

In this example, a JavaScript function is placed in <head> section of HTML page. The function is invoked (called) when a button is clicked: 

<!DOCTYPE html>
function myFunction() {
  document.getElementById("demo").innerHTML = "Paragraph changed.";

<h2>JavaScript in Head</h2>

<p id="demo">A Paragraph.</p>

<button type="button" onclick="myFunction()">Try it</button>



Hope you find it helpful.
Enjoy coding!


Post a comment

Hi, thank you for taking the time to leave a comment :)

Popular posts