Automate button click [closed]

1

I need to automate the action of a button click by some script (Javascript), could you help me?

<!doctype html>
<html lang="en" class="no-js">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="css/reset.css"> 
    <link rel="stylesheet" href="css/style.css"> 

</head>

<body class="glitch-transition">
<main class="cd-main-content">
    <div class="center">
        <a href="#modal-1" class="cd-btn cd-modal-trigger">BOTÃO</a>
    </div>
</main>

<div class="cd-modal" id="modal-1">
    <img src="img/imagem.png" />
</div> <!-- .cd-modal -->

<div class="cd-transition-layer" data-frame="25"> 
    <div class="bg-layer"></div>
</div> <!-- .cd-transition-layer -->

<script src="js/modernizr.js"></script> <!-- Modernizr -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script></script><scriptsrc="js/main.js"></script> <!-- Resource jQuery -->
</body>

</html>
    
asked by anonymous 16.08.2017 / 14:26

4 answers

1

Since you already have jQuery and need the button click on page load, you can use

$(window).ready(function(){
      // o que o botao faz
});

Detail:

The ready function is only called when the page is already loaded and ready to execute javascript

    
16.08.2017 / 14:58
0

As you already have jquery imported, you can use a tag script before the body.

First declare a tag button in your HTML document.

<html>
  ...
  <button id="seuId" />Botão</button>
</html>

Then create the script tag before the body:

<script>
   $("#seuId").click(function() { /* execute seu comando aqui */ });
</script>
    
16.08.2017 / 14:40
0

To automate the click of the button I would do the following:

First add an id to your button

<a href="#modal-1" id="botao" class="cd-btn cd-modal-trigger">BOTÃO</a>

Then inside a .js file or even inside a tag add this code:

 <script>
var botao = document.getElementById('botao');
botao.click();
</script>

Remember to put this script at the bottom of the page.

    
16.08.2017 / 14:57
0

Create a JavaScript function, and call it with onload in body

<body onload="funcao()">
    
16.08.2017 / 15:34