Javascript performs the function without the button being clicked

Asked

Viewed 102 times

0

Goal

Replace the content of the widget <P> by a new text by clicking a button.

Problem

Whenever the page loads, the javascript executes the function and changes the text without the button being clicked.

Attempt to resolve

I housed the Id variables and created a function that would include the text hello world in the paragraph, outside the function called the variable btn (button) includes the onclick and assigned the function mudaTexto.

HTML

<style>
  .lol{
    border: 1px solid black;
    width: 150px;
    height: 35px;
}
</style>

<button id="btn1">1</button>

<br><br>

<div class="lol">
  <p id="teste">hhh</p>
</div>

JS

let btn = document.getElementById('btn1');
let texto = document.getElementById('teste');

function mudaTexto(){
texto.innerText = 'hello world';
}

btn.onClick = mudaTexto();

I appreciate the help beforehand.

  • You do not need to put "solved" in the title or add the code to the question (because it is already in the answer below and it is redundant to put it here). By accepting an answer (clicking on ) you have already indicated that the problem is solved

3 answers

2

Remove the call from the function btn.onClick = mudaTexto(); of the script code and assign it directly on the button, thus:

<button id="btn1" onclick="mudaTexto();">1</button>

This way you’re adding the direct click event to the button.

2

The problem is that you are not assigning the function to the event onclick, but by invoking the function. In addition, there is also a typo. See:

btn.onClick = mudaTexto();

What you need to do is, change onClick for onclick and stop executing the function during the assignment:

btn.onclick = mudaTexto;

In this way, you will be assigning the function to the Listener of the event, not its outcome, as was being done previously.

  • 1

    It won’t work. It’s not onClick, but onclick.

  • Typo... Thank you! :)

  • When I remove the parentheses the code does not work, already with the parentheses javascript executes the function as soon as it loads the page, the button only works correctly when I call the function directly on the tag button, but I want to know how to run in the code itself.

  • Edit your question with the codes that were passed here, if you want, you can put what I passed, for us to analyze better.

1


Actually, there was a syntax error. I fixed it, see the demo below:

let btn = document.getElementById('btn1');
let texto = document.getElementById('teste');

function mudaTexto(){
texto.innerText = 'hello world';
}
//Erro de sintaxe:
//btn.onClick = mudaTexto();

//Sintaxe correta:
btn.onclick = mudaTexto;
<style>
  .lol{
    border: 1px solid black;
    width: 150px;
    height: 35px;
}
</style>

<button id="btn1">1</button>

<br><br>

<div class="lol">
  <p id="teste">hhh</p>
</div>

  • 1

    Syntax error and the fact that it is executing the function in the assignment. It is not only an error.

  • When I remove the parentheses the code does not work, already with the parentheses javascript executes the function as soon as it loads the page, the button only works correctly when I call the function directly on the tag button, but I want to know how to run in the code itself.

  • You clicked the blue button above "Run" and tested?

  • Now I realized my mistake, I wrote "onClick" instead of "onclick". Now it works correctly.

Browser other questions tagged

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