Events

Events

The actions that the user performs or the changes you make in the page are known under the name of events

An event is the sign that 'something' has happened

Syntax of events

You need to specify a selector, to know which element the event will interact with, followed by the event in parentheses

We will use the following sample HTML, that contains a simple form, to get to know the use of the events, accompanied by a file that we will call event.js

To process the form, we have also added a file form.php however, we are not going to use the examples on events

Item events

This type of event is that normally used by the user to interact with the elements of the HTML, when you hover the mouse over the element or is pressed, among others

Among the most important of which we highlight:

  • focus

    is triggered when the focus goes to the element

  • blur

    is triggered when the focus goes to another element or lose it

  • click

    is activated when you click on the element

  • dblclick

    is activated when you double click on the element

In the example we have selected the element with the id button, and responds to the click event that has as a parameter the variable warning

It could have included the code inside the click event, but wanted to emphasize that the parameters can be a variable that contains a function, so we can have different variables with different behavior, without having to modify our code

Mouse events

Occur when the user uses the mouse within the HTML document or any of the elements that compose it

Among the most important of which we highlight:

  • mousedown

    turns on when you press a mouse button

  • mouseup

    is activated when you stop pressing a mouse button

  • mousemove

    is triggered when the mouse moves

  • mouseover

    is triggered when the mouse moves over an item (when we enter in the item)

  • mouseout

    is triggered when the mouse leaves the element (when we leave the element)

In the example, we have added a couple of events that change the type of letter when you pass the mouse over the element with id button

It is possible to assign to these event parameters that contain information about a mouse event

Among the most important of which we highlight:

  • screenX and screenY

    represent the coordinates of the mouse pointer with respect to the global coordinates of the screen

  • clientX and clientY

    represent the coordinates of the mouse pointer with respect to the browser window

  • button

    is the number of the button that has been pressed (left = 0, center = 1, right = 2)

    If you do not want to pass it as a parameter, we can use the property buttonsthat includes more buttons, as the mouse wheel or the forward and back buttons of the browser

In the example we have modified the variable warning to detect which button has been clicked and what position you had the mouse on the screen, when it has been clicked

Keyboard events

Occur when the user uses the keyboard

Among the most important of which we highlight:

  • keydown

    turns on when you press a key

  • keyup

    is activated when you stop pressing a key

  • keypress

    is activated when you press and stops the press of a key

It is possible to assign to these event parameters that contain information about the event from the keyboard

Among the most important of which we highlight:

  • char or charCode

    is a string with the character that has been pressed (provided that the same is representable)

  • key or keyCode

    it is an identifier that represents the key that has been pressed

    You can see the complete list on the page for developers Mozilla

  • ctrlKey,, shitKey and altKey

    these are fields that indicate whether the key Control, Shift or Alt key had been pressed when the event occurred keyboard

In the example has been added to the variable key with parameters to capture the keystrokes when you type in the element with the id login, and has modified the set of calls to use, using the keypress event

Touch events

These events are produced by touching a touch screen

Are similar to those produced by a mouse, but its delay is much lower

Because of this, conflicts can occur with mouse events, this is why it is advisable to disable them, not to run similar events twice

Among the most important of which we highlight:

  • touchstart

    is activated when the finger touches the surface of the screen

  • touchend

    is activated when the finger stops pressing the surface of the screen

  • touchmove events

    is activated when the finger slides on the surface of the screen

  • touchenter

    is activated when the finger is moved over an item (when we enter in the item)

  • touchleave

    is triggered when the finger leaves the element (when we leave the element)

  • touchcancel

    is activated when the finger goes outside the bounds of the browser window

Form events

The forms have their own events, among the most important of which we highlight:

  • click

    it is activated when we click the mouse on an item
    It is usually used with buttons (button, submit)

  • change

    is triggered when the user modifies the value of a text element

    This is normally used with input of type text, textarea or drop-down lists to select an option

  • focus

    is activated when the user selects a form element

  • blur

    is triggered when the user moves to another form element

  • submit

    is activated when the user performs the action of submitting the form (not just when pressed the button submit, which can be simulated at the push of a button which makes the time of submit or when you force a call to the event from another event)

    In addition, the submit event generates the events mousedown, click, mouseup and submitin that same order, which allows us to perform validations on the form elements before final submission

  • reset

    is activated when the user performs the action of resetting the form (not only when you press the reset button, which can be simulated at the push of a button that makes the times of reset, or when force a call to the event from another event)

In the example, we have added the variables to send (to validate the fields login and pass) and write (to validate that it does not enter the @ symbol into the field, pass); has modified the set of calls to use, using the events submit and change