Automate click of button

Asked

Viewed 1,118 times

1

I need to automate the action of a button click for some script (Javascript), could 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>
<script src="js/main.js"></script> <!-- Resource jQuery -->
</body>

</html>
  • See if that one question helps you

  • I think I expressed myself badly, in fact, I do not know how to make the script work automatically, without going through the click of the button, in fact, when the page is loaded, it is necessary to run automatically this click of the button to which appears on the line <a href="#modal-1" class="cd-btn cd-modal-Trigger">BUTTON</a> I don’t know how to do this. I’ve studied some codes and nothing.

  • It would help to put Javascript code

4 answers

1

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.

  • Didn’t it work ... something to do with the href attribute of the <a></a> tag? Is there another tag I can use instead of the <a> one? The script I used both at the beginning (before the body) and at the end. Also did not roll

  • Hmm might be. You’re using some library/framework to use these modals?

  • I use Modernizr.js This is my current script found in my main.js, where I do the modal references: /open modal window modalTrigger.on('click', Function(Event)' Event.preventDefault(); var modalId = $(Event.target). attr('href'); transitionLayer.addClass('Visible Opening'); var delay = ( $('. no-cssanimations'). length > 0 ) ? 0 : 800; setTimeout(Function(){ modalWindow.filter(modalId). addClass('Visible'); transitionLayer.removeClass('Opening'); }, delay); });

  • try placing this script after the main.js call, for example: <script src="js/main.js"></script> <script> var botao = Document.getElementById('botao'); botao.click(); </script> or try to swap the a tag with a button

  • or if you prefer put the code I gave you at the end of your main.js file

1

Since you already have jQuery and need to click the button on the 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 for javascript execution

0

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

First declare a tag button in your HTML document.

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

Then create the script tag before body:

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

0

Create a Javascript function, and call it with onload in the body

<body onload="funcao()">

Browser other questions tagged

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