Simulate Click in Javascript

Asked

Viewed 7,149 times

-2

I found a code that simulates the click of a button, but I need to call a function in javascript and pass a string, how would that look ?

document.getElementById("meuElemento").click();

  • 1

    Can you explain better what you want to do? You know what it is addEventListener? explain what you want to do to make it clearer

  • I don’t know if this is what you want but to call a function by clicking the button put this in your tag button onclick="funcao('string passada')", and in javascript I put the function like this function funcao(recebestring)

  • @abduzeedo you want the function to be called when there is a click or you want to click because you called a function?

  • @abduzeedo I want to realize and help but neither I nor the 3 who responded seem to have hit/realized. You can give more examples/explanation to see if it is clear to us?

  • Okay, I have a button on my page, the ID of this button is Meubotao, okay. I want the visitor not to have to click on this button, I want a javascript script to trigger the ONCLICK event from this button automatically, filtering by the button ID

2 answers

1

You can call the function directly as well:

function evento(string){
  console.log(string);
}
<input type="button" id="meuElemento" onclick="evento('Teste')">

0

put it like this:

  <a href="#" id="meuElemento" onclick="funcao('minha string')">clique aqui.</a>

        <script>
            function funcao(string) {
                alert(string)
            }
            document.getElementById("meuElemento").click();
        </script>
  • that is, Document.getElement is simulating the click. in the link click there is the function that passes a string. inside the function vc manipulates the string.

  • I’ll edit to make it easier for you to understand.

  • testa la brother

Browser other questions tagged

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