How to make a button that runs 2 events?

Asked

Viewed 70 times

0

Hello,

Then I would like to know how I can make a button on which when clicking for the first time it changes the content of a SPAN and the time I click again it goes back to the old value.

  • That is more or less what you want to do, but with 2 buttons, but already gives a north.

1 answer

3


There are several ways to do this and normally you won’t need to make the button run more than one event.

A simple way is to always keep the next value saved in a variable.

document.getElementById('bt-toggle').addEventListener('click', fnClick);

var proximoValor = 'Teste 2';
function fnClick() {
  var span = document.getElementById('span');
  
  var aux = span.innerText;
  span.innerText = proximoValor;
  proximoValor = aux;
}
<span id="span">Teste 1</span> <br>
<button id="bt-toggle">Toggle</button>

Browser other questions tagged

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