I want my code to run when I type a number into my input, but every time I reload the page the code automatically fires.
const dinero = document.getElementById('ValorGanancia');
dinero.addEventListener('keypress', alert('hola mundo'));
<div class="CalculoCalculadora-IngresoTotal">
<p>Bill </p>
<input type="number" name="ValorGanancia" id="ValorGanancia" placeholder="0" value="">
<span class="Logopesos"> <img class="imgPesos" src="./images/icons8-dólar-estadounidense-24.png" alt="" srcset=""></span>
</div>
When adding the event do not directly use
alert
. You can wrap the code you want to execute when the event fires in an anonymous function, in this case an arrow function:or with a normal anonymous function:
Something simpler, you could do it in the following way:
Or you could also use an anonymous function:
You should do the following: