Javascript Events

Javascript Events

Javascript Events


Events are things, which happen with HTML or Javascript variable.

Javascript can catch (react) that thing (events).

Javascript Events

Javascript events can be triggered by the user or by the browser. Let's imagine a problem, that you need to call a function, when the HTML tag clicked or any other events.

To explain it, you must know, that you can use Javascript Events in two ways.

1) With Html Tags

2) With Javascript Codes

Here is the syntax of using events.

<any-tag any-event="Javascript code">

 object.[any-event]=function(){myScript}; 

 object.addEventListener("[any-event]", myScript); 

Let's make an example.

<!DOCTYPE html>
<html>
 <body>
   <button onclick="getElementById('element').innerHTML='click-event'">Click Me</button>
   <p id="element">Test me</p>
 </body>
</html>

Javascript has a very long list for Events. We'll try to show you the basics, which you will need.

This is the HTML tag events list

Mouse Events

AttributeValueDescription
onclickscriptFires on a mouse click on the element
ondblclickscriptFires on a mouse double-click on the element
ondragscriptScript to be run when an element is dragged
ondragendscriptScript to be run at the end of a drag operation
ondragenterscriptScript to be run when an element has been dragged to a valid drop target
ondragleavescriptScript to be run when an element leaves a valid drop target
ondragoverscriptScript to be run when an element is being dragged over a valid drop target
ondragstartscriptScript to be run at the start of a drag operation
ondropscriptScript to be run when dragged element is being dropped
onmousedownscriptFires when a mouse button is pressed down on an element
onmousemovescriptFires when the mouse pointer is moving while it is over an element
onmouseoutscriptFires when the mouse pointer moves out of an element
onmouseoverscriptFires when the mouse pointer moves over an element
onmouseupscriptFires when a mouse button is released over an element
onmousewheelscriptDeprecated. Use the onwheel attribute instead
onscrollscriptScript to be run when an element's scrollbar is being scrolled
onwheelscriptFires when the mouse wheel rolls up or down over an element

Form Events

AttributeValueDescription
onblurscriptFires the moment that the element loses focus
onchangescriptFires the moment when the value of the element is changed
oncontextmenuscriptScript to be run when a context menu is triggered
onfocusscriptFires the moment when the element gets focus
oninputscriptScript to be run when an element gets user input
oninvalidscriptScript to be run when an element is invalid
onresetscriptFires when the Reset button in a form is clicked
onsearchscriptFires when the user writes something in a search field (for <input="search">)
onselectscriptFires after some text have been selected in an element
onsubmitscriptFires when a form is submitted

Keyboard Events

AttributeValueDescription
onkeydownscriptFires when a user is pressing a key
onkeypressscriptFires when a user presses a key
onkeyupscriptFires when a user releases a key

Lets make some examples.

<!DOCTYPE html>
<html>
  <body>

   <p>Here is W3docs.com keypress Events exmple</p>
   <input type="text" onkeypress="myFunction()">

   <script>
    function myFunction() {
      alert("Hello onkeypress event");
    }
   </script>
 </body>
</html>
<!DOCTYPE html>
<html>
  <body>

   <button ondblclick="myFunction()">W3docs.com Dbclick Event</button>
   <p id="element">Text will change</p>

   <script>
     function myFunction() {
        document.getElementById("element").innerHTML = "Hello World";
     }
   </script>

  </body>
</html>
<!DOCTYPE html>
<html>
  <body>

   <p id="element" onmousedown="mDown()" onmouseup="mUp()">
          The text will be colored when you click on it
   </p>

   <script>
     function mDown() {
        document.getElementById("element").style.color = "blue";
     }

     function mUp() {
       document.getElementById("element").style.color = "red";
     }
   </script>
  </body>
</html>
Reactions

Post a Comment

0 Comments

close