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

Asked

Viewed 400 times

0

I made a button to open a configuration screen, when I click open the screen, and the button changes from "Setup" for "Come back", until then beauty, but I would like that when it clicked again, this writing again "Setup".

Can anyone help me? Preferably at jQuery.

$("#settings").hide();
$("#principal").show();
    $("#config").click(function(){
        $("#settings").toggle();
            $("#principal").toggle();
            $(this).html("Voltar");

1 answer

1


You can add a condition. If the element #settings is visible, shows a given name, otherwise 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");
}

Follows 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>

<div id="settings">settings</div>
<div id="principal">principal</div>

<button id="config">Configurações</button>

Browser other questions tagged

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