What would be the 'e' passed as a parameter in js functions?

Asked

Viewed 255 times

13

What would be the 'e' that is passed as a function parameter? Ex.:

function nome(e) {
    (instrução);
}

Does anyone have any material, or keyword for research that I can turn to to study a little more on the subject?

  • good sites to study would be: http://www.codecademy.com/pt-BR/tracks/javascript and http://www.codecademy.com/pt-BR/tracks/jquery

2 answers

20


  • 1

    Thanks a lot, Sergio! Abs.

  • 1

    I get it,,, vlw man...

3

In this case this 'e' is a specific parameter in specific functions, such as events.

It is very common in jQuery. For example, when we create an onclick at a link:

$('#link-legal').on('click', function(e) {
    alert('Link legal clicado!');
});

In this case, this 'and' parameter (which can actually have any name, but the letter 'and' which is short for 'Event') will contain the link object clicked with several properties and methods.

Application example

Let’s say you want, when clicked on a link, to be asked a confirmation to the user and, depending on its response, the click is 'canceled':

$('a.confirmar').on('click', function(e) {
    var confirmado = confirm('Deseja realmente prosseguir para esse link?');
    if(confirmado == false) {
        e.preventDefault();
        alert('Click cancelado');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="https://www.google.com" target="_blank" class="confirmar">Ir para o Google</a>

In this example we use the method preventDefault which cancels the standard action of the element, which in the case of the tag <a> is the path to the url indicated in the property href.


Usually in the documentations you find this type of reference, as you can check in the official documentation of jQuery for the event click:

https://api.jquery.com/click/

There it says (in free translation):

.click( Handler ) Handler Type: Function( Event eventObject ) A function that will be executed at each event trigger.

You can see in the guy it says that a Function in which an object parameter of the type is passed Event.

  • 1

    Just remember that the object passed by jQuery is not equal to the native JS event, but a normalized version. Or at least it used to be like this, they may have changed in the newer versions of jQuery.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.