HTML & JavaScript

Author: MDBootstrap

JavaScript is a programming language that makes HTML pages more dynamic and interactive.

Of course, JavaScript is a very powerful technology and can be used for much more purposes, but for this moment we stay by the one mentioned above.

Note: This lesson is only a short introduction to JavaScript. You will learn more about it in the next tutorials. The purpose of this lesson is just to explain to you what JavaScript is.

                <h1>My First JavaScript</h1>
                <button type="button"
                onclick="document.getElementById('demo').innerHTML = Date()">
                Click me to display Date and Time.</button>
                <p id="demo"></p>

Live preview

My First JavaScript

The HTML <script> Tag

The <script> tag is used to define a client-side script (JavaScript).

The <script> element either contains scripting statements, or it points to an external script file through the src attribute.

Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.

To select an HTML element, JavaScript very often uses the document.getElementById() method.

              <h2>Use JavaScript to Change Text</h2>
              <p>This example writes "Hello JavaScript!" into an HTML element with id="demo-1":</p>
              <p id="demo-1"></p>
                document.getElementById("demo-1").innerHTML = "Hello JavaScript!";

Live preview

Use JavaScript to Change Text

This example writes "Hello JavaScript!" into an HTML element with id="demo-2" (red box below):

Note: Tip at the end - do not confuse JavaScript with Java. These are completely different technologies that serve completely different purposes.

Previous lesson Next lesson

Spread the word:
Do you need help? Use our support forum

About the author