Home HTML CSS JavaScript React Gamedatum.com
× Home HTML CSS JavaScript React Gamedatum.com

JS Introduction


JavaScript is the most popular programming language in the world and will allow you to utilise libraries that can take your web development project to the next level.

How to insert Javascript into the page

The First way:

The Javascript code is displayed inside the opening and closing script tags:

<script></script> This way you can embed Javascript directly in the source code of the page.

Another way is by inserting a script link into the page such as below:

The Second way:

Create a new file and save it as a javascript file using the extension .js and then insert the link to its location inside the src attribute.

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

The things you can do with Javascript are lots. JavaScript is also known as the language of the web.

With Javascript you manipulate the elements of the page and create an interactive user interface (UI).

Examples of the Javascript use on a page are displayed below:

Change the text of a div on the click of a button

               
                
    <!-- The Button with the onclick event -->
    <button onclick="changeMe()">Click me</button>
    <div id="change">I am random text.</div>
    <!-- The Javascript code -->
    <script> 
   function changeMe(){ 
   document.getElementById("change").innerHTML = "Hi! I'm the new text.";
   }
    </script>
                
                
              

The result:

I am random text.

Change the color of a div on the click of a button

               
                
    <!-- The Button with the onclick event -->
    <button onclick="changeColor()">Click me</button>
    <div id="color">Change my color to blue!</div>
    <!-- The Javascript code -->
    <script> 
   function changeColor(){ 
   document.getElementById("color").style.color = "blue";
   }
    </script>
                
                
              

The result:

Change my color to blue!