function clickbutton without jquery

3

I have a code that clicks on a button, but I want to do the same without jquery, since jquery makes my page very slow, like I do with javascript? this is the code:

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
    $(document).ready(function()
        setTimeout(clickbutton,5000);

        function clickbutton()
        {
            $("#botonEnviar").click();
        }
    });
</script>
    
asked by code2018 29.07.2018 в 04:24
source

2 answers

1

I leave this example, I hope it will help you get oriented for your situation

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<button id="btn">Hola</button>
<script>
    window.addEventListener("DOMContentLoaded", function(){
      setTimeout(function(){
         clickbutton()
      }, 5000)
    })
    
    function clickbutton(){
      let btn = document.querySelector("#btn")
      btn.innerHTML = "Me diste click"
    }
</script>
</body>
</html>

I use a listener to listen when the DOM is "ready" through DOMContentLoaded and then I invoke the function setTimeout that inside receives the name of my custom function and the assigned time.

Outside of reading the DOM , I develop my custom function, where with querySelector() I get the id of the HTML element I want to obtain

For example purposes with the innerHTML method I modify the content of button to show the functionality of the newly created code

  

In the case of being able to check when the DOM is loaded, it is   available in this way

self.addEventListener ...
document.addEventListener ...
window.addEventListener ...
    
answered by 29.07.2018 в 05:32
0

There are many ways to do it, but I would like to mention that jQuery is a very lightweight library and I do not think it's jQuery that makes it slow, maybe you're calling several CSS styles.

But to the question, how to do what you have without jQuery. The way I do it:

In the body (this causes the clickbutton function to run when the page is opened.

<body onload="clickbutton()">

Then in the html of your button, suppose you have something like that

<button id="botonEnviar" onclick="clickbutton()">Enviar</button>

Then your script

<script>
        function clickbutton()
        {
            //todo tu código de la función
        }
</script>
    
answered by 29.07.2018 в 05:16