top

Search

JavaScript Tutorial

There are many events available for JavaScript to change something. When JavaScript was first created, it used to add these little features on the webpage. Like someone clicks at some button, or the page has loaded.We will look at the below, most important events and event example here.onclickThe onclick event occurs when the user clicks on some elements. There are three ways to implement onclick or any other event.By HTMLWe can implement any event in HTML. From any element like the <p> tag, we have onclick opening a function. We then implement the function inside <script> tag to show a text in innerHTML of a tag.<!DOCTYPE html> <html> <body>    <h1 id="demo"></h1>    <p onclick="myFunction()">Click me.</p>    <script>        function myFunction() {            document.getElementById("demo").innerHTML = "The result of clicking";        }    </script> </body> </html>Save the above code in a .html file and open in any web browser. It will show the below webpage.Before ClickingClick me.After ClickingThe Result of clickingClick me.By JavaScriptWe can implement the same using JavaScript. Here onclick event goes inside the <script> tag and so do all the logic. We are having two different ids here. In the “click” id we are attaching onclick event and then the function myFunction get calls when someone clicks on it. Here we show a text in innerHTML of the <h1> tag.<!DOCTYPE html> <html> <body>    <h1 id="demo"></h1>    <p id="click">Click me.</p>    <script>        document.getElementById("click").onclick = function () { myFunction() };        function myFunction() {            document.getElementById("demo").innerHTML = "The result of clicking";        }    </script> </body> </html>The result will be the same as with the HTML when opened in a webpage.By JavaScript using addEventListener()We can implement the same using JavaScript’s addEventListener() method. In fact, it is the most common way to do it. Here we add the Event Listener method to the element, which we select by document.getElementById("click"). Inside it, we have two parameters – one the click event and other a callback function.<!DOCTYPE html> <html> <body>    <h1 id="demo"></h1>    <p id="click">Click me.</p>    <script>        document.getElementById("click").addEventListener("click", myFunction);        function myFunction() {            document.getElementById("demo").innerHTML = "The result of clicking";        }    </script> </body> </html>OnchangeThe onchange event occurs when the value of an element is changed like we change something in an input box, or in a select box or radio box.<!DOCTYPE html> <html> <body>    Enter some text: <input type="text" onchange="myFunction(this.value)">    <script>        function myFunction(val) {            alert("The new value is: " + val);        }    </script> </body> </html>Save the above code in a .html file and open in any web browser. It will show the below webpage. Write something in the input box and click somewhere outside, you will get an alert box with the value you entered.onmouseover and onmouseoutThe onmouseover event occurs when the user moves the mouse over the targeted element. Opposite to it, the onmouseout event occurs when the user moves the mouse away from the targeted element.Both these event handlers are generally used together. Below is an example using addEventListener way. Here we are adding two event listener, which fires two events. The mouseover event changes the color of an <h1> tag to green and the mouseout event changes. It back to black.<!DOCTYPE html> <html>  <body>    <h1 id="demo">Mouse over me</h1>    <script>      document.getElementById('demo').addEventListener('mouseover', mouseOver);      document.getElementById('demo').addEventListener('mouseout', mouseOut);      function mouseOver() {        document.getElementById('demo').style.color = 'green';      }      function mouseOut() {        document.getElementById('demo').style.color = 'black';      }    </script>  </body> </html>Save the above code in a .html file and open in any web browser. It will show the below webpage. Move the mouse over to the text and it will change color to red. Move the mouse out and the color changes back to black.OnloadThe onload event in javascript occurs when an object has been loaded. onload is most often used within the <body> element to execute a script, once the whole page is loaded.In the example below, we change the color and font-size of an element once the page is fully loaded<!DOCTYPE html> <html>  <body onload="myFunction()">    <div id="demo">Hello World!</div>    <script>      function myFunction() {        document.getElementById('demo').style.color = 'green';        document.getElementById('demo').style.fontSize = '52px';      }    </script>  </body> </html>
logo

JavaScript Tutorial

JavaScript Events

There are many events available for JavaScript to change something. When JavaScript was first created, it used to add these little features on the webpage. Like someone clicks at some button, or the page has loaded.

We will look at the below, most important events and event example here.

onclick
The onclick event occurs when the user clicks on some elements. There are three ways to implement onclick or any other event.

By HTML

We can implement any event in HTML. From any element like the <p> tag, we have onclick opening a function. We then implement the function inside <script> tag to show a text in innerHTML of a tag.

<!DOCTYPE html>
<html>
<body>
   <h1 id="demo"></h1>
   <p onclick="myFunction()">Click me.</p>
   <script>
       function myFunction() {
           document.getElementById("demo").innerHTML = "The result of clicking";
       }
   </script>
</body>
</html>

Save the above code in a .html file and open in any web browser. It will show the below webpage.

Before Clicking

Click me.

After Clicking

The Result of clicking

Click me.

By JavaScript

We can implement the same using JavaScript. Here onclick event goes inside the <script> tag and so do all the logic. We are having two different ids here. In the “click” id we are attaching onclick event and then the function myFunction get calls when someone clicks on it. Here we show a text in innerHTML of the <h1> tag.

<!DOCTYPE html>
<html>
<body>
   <h1 id="demo"></h1>
   <p id="click">Click me.</p>
   <script>
       document.getElementById("click").onclick = function () { myFunction() };
       function myFunction() {
           document.getElementById("demo").innerHTML = "The result of clicking";
       }
   </script>
</body>
</html>

The result will be the same as with the HTML when opened in a webpage.

By JavaScript using addEventListener()

We can implement the same using JavaScript’s addEventListener() method. In fact, it is the most common way to do it. Here we add the Event Listener method to the element, which we select by document.getElementById("click"). Inside it, we have two parameters – one the click event and other a callback function.

<!DOCTYPE html>
<html>
<body>
   <h1 id="demo"></h1>
   <p id="click">Click me.</p>
   <script>
       document.getElementById("click").addEventListener("click", myFunction);
       function myFunction() {
           document.getElementById("demo").innerHTML = "The result of clicking";
       }
   </script>
</body>
</html>

Onchange

The onchange event occurs when the value of an element is changed like we change something in an input box, or in a select box or radio box.

<!DOCTYPE html>
<html>
<body>
   Enter some text: <input type="text" onchange="myFunction(this.value)">
   <script>
       function myFunction(val) {
           alert("The new value is: " + val);
       }
   </script>
</body>
</html>

Save the above code in a .html file and open in any web browser. It will show the below webpage. Write something in the input box and click somewhere outside, you will get an alert box with the value you entered.

The new value is hello in JavaScript

onmouseover and onmouseout

The onmouseover event occurs when the user moves the mouse over the targeted element. Opposite to it, the onmouseout event occurs when the user moves the mouse away from the targeted element.

Both these event handlers are generally used together. Below is an example using addEventListener way. Here we are adding two event listener, which fires two events. The mouseover event changes the color of an <h1> tag to green and the mouseout event changes. It back to black.

<!DOCTYPE html>
<html>
 <body>
   <h1 id="demo">Mouse over me</h1>
   <script>
     document.getElementById('demo').addEventListener('mouseover', mouseOver);
     document.getElementById('demo').addEventListener('mouseout', mouseOut);
     function mouseOver() {
       document.getElementById('demo').style.color = 'green';
     }
     function mouseOut() {
       document.getElementById('demo').style.color = 'black';
     }
   </script>
 </body>
</html>

Save the above code in a .html file and open in any web browser. It will show the below webpage. Move the mouse over to the text and it will change color to red. Move the mouse out and the color changes back to black.

Mouse over me in javaScript

Onload

The onload event in javascript occurs when an object has been loaded. onload is most often used within the <body> element to execute a script, once the whole page is loaded.
In the example below, we change the color and font-size of an element once the page is fully loaded

<!DOCTYPE html>
<html>
 <body onload="myFunction()">
   <div id="demo">Hello World!</div>
   <script>
     function myFunction() {
       document.getElementById('demo').style.color = 'green';
       document.getElementById('demo').style.fontSize = '52px';
     }
   </script>
 </body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Janani

I have learned many things from this article. It is beneficial for me. Thank you!

Nilesh Chakrabarty

Nice example for beginners.. I m a beginner so this is very helpful for me ... so plz give this type of beginners example..

michael

This is a great introduction to variables in JavaScript! As a beginner to JavaScript, I found this guide very helpful in understanding the basics of variables and how they are used in JavaScript.

qtsinfo it

Thanks for sharing the information, it is very helpful, I hope to get more such beautiful blogs from you.

knowledge-wisdom

You have shared great information with me i appreciate your work!

Suggested Tutorials

Node JS Tutorial

Build various types of web applications,command-line application,etc.
Node JS Tutorial

Build various types of web applications,command-line application,etc....

Read More

Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introduced by Google corporation in 2012 and was considered to be one of the most promising among JavaScript frameworks. It was written completely in JavaScript to separate an application’s logic from DOM manipulation, aiming at dynamic page updates. Angular introduced many powerful features enabling the developer to effortlessly create rich and single-page applications.Topics CoveredThis Angular tutorial will span over eight modules, each module covering numerous individual aspects that you need to gain complete information about Angular. This set of modules serves as an Angular tutorial for beginners along with experienced IT professionals.Here are the topics that will be covered in the Angular tutorial:Get started with Angular.Learn the basics of Angular.Know what Angular Directives.Get an idea of Component Inputs and Outputs of Angular.Know about Forms in Angular.About Services in Angular.Pipes in Angular.HTTP, Routing and Building in Angular.Who can benefit from this tutorial?This Angular tutorial will be helpful to IT professionals such as:Software Developers, Web Application Programmers and IT Professionals Software Architects and Testing Professionals Career aspirants in web development
Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introdu...

Read More