How do I fire an Alert with the click of a button in javascript?

Asked

Viewed 48 times

-5

let iniciar = document.getElementById("iniciar")
let pausar = document.querySelector('.pausar')

iniciar.addEventListener("click", function(event) {
  alert('Cliquei no iniciar')
}, true);

pausar.addEventListener('click', function(event) {
  alert('Cliquei no pausar')
});
<button id='iniciar'>Iniciar</button>
<button class='pausar'>Pausar</button>

2 answers

2


Complementing the answer of pedro, it is nice to separate Javascript from html. It would look like this:

document.getElementById("btnAlerta").addEventListener('click', MeuAlerta);

function MeuAlerta() {
  alert('Escreva a mensagem');
}
<button type="button" id="btnAlerta">Alerta</button>

-2

Try this:

function MeuAlerta() {
  alert('Escreva a mensagem');
}
<button type="button" id="btnAlerta" onchange="MeuAlerta();">Alerta</button>

Browser other questions tagged

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