Return two functions in a JS onclick event

Asked

Viewed 35 times

-2

My question is simple, suppose I have a button, when I once click on it I want to run a function as you can see below:

function ativar() {
  var btn = document.getElementsByTagName("button");
  btn[0].innerHTML="ativado";
}

function desativar() {
  var btn = document.getElementByTagName("button");
  btn[0].innerHTML="desativado";
}
<html>
  <head>
    <title>exemplo</title>
  </head>
  
  <body>
    <button onclick="ativar()">desativado</button>
  </body>
</html>

see that the second function is not used, there is the problem, suppose I activated the button, giving a click, ran the function activate(), but I want when I give a new click, it runs the second function disable().

1 answer

2

You can try it:

var stateButton = false;

function ativar() {
  var btn = document.getElementsByTagName("button");
  if ( stateButton == false ) {
    btn[0].innerHTML="ativado";
    stateButton = true;
  } else {
    btn[0].innerHTML="desativado";
    stateButton = false;
  }
}
  <button onclick="ativar()">desativado</button>

  • Thanks, it helped me a lot, unfortunately there are people who negative the question without even knowing the context.

Browser other questions tagged

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