Click and act, click again and return to position / Jquery

0

I made a button to open a configuration screen, when I click open the screen, and the button changes from "Configuration" to "Back" but I would like it when I clicked again to write "Configuration" .

Can anyone help me? Preferably in jQuery .

$("#settings").hide();
$("#principal").show();
    $("#config").click(function(){
        $("#settings").toggle();
            $("#principal").toggle();
            $(this).html("Voltar");
    
asked by anonymous 17.02.2018 / 20:36

1 answer

1

You can add a condition. If the #settings element is visible, it shows a particular name, otherwise it displays another name. Ex:

/* Caso a div #settings esteja visível, escreve "Voltar" */
if ($("#settings").is(":visible")) {
  $(this).html("Voltar");
}
/* Caso contrário, escreve "Configurações" */
else {
  $(this).html("Configurações");
}

Following code:

$("#settings").hide();
$("#principal").show();

$("#config").click(function() {
  $("#settings").toggle();
  $("#principal").toggle();
  
  if ($("#settings").is(":visible")) {
    $(this).html("Voltar");
  } else {
    $(this).html("Configurações");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="settings">settings</div>
<div id="principal">principal</div>

<button id="config">Configurações</button>
    
17.02.2018 / 20:49