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
– hkotsubo