How to do this in pure Javascript?

Asked

Viewed 688 times

-1

How do I do it in pure JS?

$(function () {
    $("#selCurso").change(function () {
        Pesquisar();
    });
});

1 answer

4

$() is a shorthand of jQuery() which, in turn, is a function that can receive different parameters and make a decision for each of them.

At the beginning, its entire function is passed as a parameter. This serves for the function to be executed only after all the DOM has been loaded and is ready for use - see documentation.

The equivalent of this would be to use the event DOMContentLoaded of the document.

document.addEventListener('DOMContentLoaded', function() { });

You can read more about this specifically at What is the difference between $(Document). ready() and window.onload? and also in Is there any equivalent of "$(Document). ready()" with pure Javascript?

In the second case, the function $() receives a selector and returns one or more elements that "meet" this selector - see documentation.
Same as CSS, #selCurso is a selector for the element that has id equal to selCurst.

The equivalent of this is document.getElementById('selCurso').
Or, in a more "modern way" document.querySelector('#selCurso').

The function change adds a Event Handler to the Javascript event called "change".

The equivalent of this is .addEventListener('change', callback).

document.addEventListener('DOMContentLoaded', function () {
    document.getElementById('selCurso').addEventListener('change', function () {
        Pesquisar();
    });
});

Browser other questions tagged

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