Is there any way to write a function on the screen other than by onclick? [closed]

Posted on

Question :

Hello, the only way I can call a javascript function in html is by onclick, but my html is not a button to click, I would like to know if there is another way to display a function when it is called.

ex: onload -> para quando a tela carregar
onclick -> quando clicarem no elemento

Are there any that just show?


Answer :

function fazerAlgo()
  //código aqui
// em alguma parte do seu código mais tarde...


<script src="minhafuncao.js"></script>
<div id="">Em alguma parte do seu html</div>


In your own question is one of the alternatives: no onload .

If you do not want to call the function through a direct action user (eg click Explicit , either during page loading, simply calling the function after the function code:

function minhaFuncao(){

Or after page load:

function minhaFuncao(){
window.onload = minhaFuncao;


There are several Listener’s that can be used, some examples and ways of application with pure JavaScript:

document.addEventListener('click', suaFuncao(), true); // No Clique
document.addEventListener('load', suaFuncao(), true); // No load
document.addEventListener('resize', suaFuncao(), true); // No Resize

and to apply them let’s suppose, in the click:

const btn = document.querySelector('classe ou id do seu botao');
btn.addEventListener('click', suaFuncao(), true);


Leave a Reply

Your email address will not be published. Required fields are marked *