Javascript Home

Events

onclick Event

In the following example, an onclick attribute (with code) is added to a <button> element.


  <button class="btn btn-primary mb-5" onclick="document.getElementById('demo').innerHTML=Date()">The time is?</button>

  <p id="demo"></p>


In the next example, the code changes the content of its own element (using this.innerHTML).

  <button class="btn btn-primary mb-5" onclick="this.innerHTML=Date()">The time is?</button>


The click event may call a function.


  <button class="btn btn-primary mb-5" onclick="displayDate()">The time is?</button>

  <p id="demo1"></p>

  <script>

    function displayDate() {

      document.getElementById("demo1").innerHTML = Date();

    }

  </script>

onchange Event

When you select a new car, a function is triggered which outputs the value of the selected car.



  <select class="btn btn-primary mb-5" id="mySelect" onchange="myFunction()">

    <option value="Audi">Audi</option>

    <option value="BMW">BMW</option>

    <option value="Mercedes">Mercedes</option>

    <option value="Volvo">Volvo</option>

  </select>

  <p id="demo2"></p>

  <script>

    function myFunction() {

      var x = document.getElementById("mySelect").value;

      document.getElementById("demo2").innerHTML = "You selected: " + x;

    }

  </script>

onmouseover And onmouseout Events

The function bigImg() is triggered when the user moves the mouse pointer over the image.

The function normalImg() is triggered when the mouse pointer is moved out of the image.




  <img onmouseover="bigImg(this)" onmouseout="normalImg(this)" border="0" src="../images/smiley.gif" width="32" height="32">

  <script>

    function bigImg(x) {

      x.style.height = "64px";

      x.style.width = "64px";

    }

    function normalImg(x) {

      x.style.height = "32px";

      x.style.width = "32px";

    }

  </script>

onkeydown Event

A function is triggered when the user is pressing a key in the input field.



  <input type="text" class="border border-primary" style="background-color:yellow" onkeydown="myFunction_1()">

  <script>

    function myFunction_1() {

      alert("You pressed a key inside the input field");

    }

  </script>

onkeypress Event

The onkeypress event occurs when the user presses a key (on the keyboard).



  <input type="text" class="border border-primary" style="background-color:yellow" id="demo3" onkeypress="myFunction_2()";

  <script>

    function myFunction_2() {

      document.getElementById("demo3").style.backgroundColor = "red";

    }

  </script>

onkeyup Event

A function is triggered when the user releases a key in the input field. The function transforms the character to upper case.



  <input type="text" class="border border-primary" style="background-color:yellow" id="fname" onkeypress="myFunction_3()";

  <script>

    function myFunction_3() {

      var x = document.getElementById("fname");

      x.value = x.value.toUpperCase();

    }

  </script>

onload Event

The onload event occurs when an object has been loaded.



  <button class="btn btn-primary mb-5" onclick="load_home()">Click to load external page</button>

  <div class="border border-primary p-3 m-3" id ="content" style="text-align: center; background-color:yellow"></div>

  <script>

    function load_home() {

      document.getElementById("content").innerHTML='<object type="text/html" data="../iframes/onload-event.html"></object>';

    }

  </script>



onload-event.html

  <body onload="myFunction()">

    <h3>Hello World!</h3>

    <script>

      function myFunction() {

        alert("Page is loaded");

      }

    </script>

  </body>