How to make a button that performs 2 events?

0

Hello,

So I would like to know how I can make a button on which when I first click it it changes the contents of a SPAN and at the time I click it it goes back to the old value.

    
asked by anonymous 06.07.2017 / 02:39

1 answer

4

There are several ways you can do this, and usually you do not 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>
    
06.07.2017 / 02:47