Javascript onclick Event Example

Apr 02, 2021 . Admin

Hello Friends,

Now let's see example of how to use javascript onclick event example. This is a short guide on onclick event. Here you will learn how to use javascript onclick event. We will use how to use if onclick event in javascript. Let's get started with how to onclick event in javascript.

Here i will give you many example how you can check javascript onclick event.

Example: 1
In this example i will use id for onclick event
<!DOCTYPE html>
<html>
<head>
   <title>Javascript - How to check if onclick event?-MyWebtuts.com</title>
</head>
<body>

    <h1>The onclick Event</h1>
    <button onclick="myFunction()">Click me</button>
    
    <p id="test"></p>
    
    <script>
    function myFunction() {
      document.getElementById("test").innerHTML = "Javascript onclick";
    }
    </script>

</body>
</html>
Output: 1
    Javascript onclick
Example: 2 In this example i will use attribute for onclick event.
<!DOCTYPE html>
<html>
<head>
   <title>Javascript - How to check if onclick event?-MyWebtuts.com</title>
</head>
<body>

    <h1>The onclick Event</h1>
    <button onclick="myFunction()">Click me</button>

    <script>

    function myFunction() {
	   alert('Button was clicked!');
    }
    </script>

</body>
</html>
Output: 1
    Button was clicked!

It will help you....

#Javascript