You can run the alert
(or any other code within the event listener
) creating a trigger click()
for the event:
document.getElementById("voltar").click();
document.getElementById("voltar").addEventListener('click', function() {
alert("teste");
}, true);
document.getElementById("voltar").click();
<input type="button" value="Voltar" id="voltar" />
You can also use a separate function:
function alerta(){
alert("teste");
}
document.getElementById("voltar").addEventListener('click', alerta, true);
function alerta(){
alert("teste");
}
document.getElementById("voltar").addEventListener('click', alerta, true);
<button type="button" id="voltar">Voltar</button>