show/hide form using javascript buttons

Asked

Viewed 492 times

2

I need to show a form using a button and hide it when the user presses another button, because the other button should show a table fed with database data. I used the following answer as a basis and managed to hide and show the form but when trying to do something similar with the table the site Buga and both are overlapped.

NOTE: I am using bootstrap 4 and jquery-3.4.1

/a/104772/154025

<?php
include ('menu.php');
?>
<link rel="stylesheet" type="text/css" href="./assets/css/painel.css">

<main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
  <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">Clientes</h1>
  </div>
  <div id="container">

     <!--
      ########################################################################################################################################################### BUTTONS ###################################################################################################################################################################-->

      <button type="button" class="btn btn-success" onclick="Mudarestado('formulario')">Novo</button>
      <button type="button"class="btn btn-secondary" onclick="#">Listar</button> 

      <!--
      ########################################################################################################################################################### FORMULARIO ################################################################################################################################################################-->

      <form id="formulario" method="POST" action="cadastrar.php">
            <div class="form-row">
                <div class="col">
                    <label for="inputNome">Nome</label>
                    <input type="text" class="form-control" id="Nome" placeholder="Primeiro Nome">
                </div>
                <div class="col">
                    <label for="inputSobrenome">Sobrenome</label>
                    <input type="text" class="form-control" id="Sobrenome" placeholder="Ultimo Nome">
                </div>
            </div>
          <div>
            <br>
            <div class="form-check form-check-inline">
              <input class="form-check-input" type="radio" name="sexo" value="Masculino">
              <label class="form-check-label" for="Radio1">Masculino</label>
            </div>
            <div class="form-check form-check-inline">
              <input class="form-check-input" type="radio" name="sexo" value="Feminino">
              <label class="form-check-label" for="Radio2">Feminino</label>
              <br>
            </div>
          </div>
            <div class="form-row">
                <div class="form-group col-md-2">
                    <label >CPF</label>
                    <input type="text" class="form-control" name="CPF" id="CPF" placeholder="000.000.000-00">
                </div>
              <div class="form-group col-md-">
                <label for="inputAddress">Endereço</label>
                <input type="text" class="form-control" id="Endereço" placeholder="RAU-SC">
            </div>
            </div>
                <div class="input-group-prepend" style="display:inline-block; vertical-align: middle;">
                  <button class="btn btn-outline-success" type="button">Savar</button>
                <button class="btn btn-outline-secondary" type="button">Cancelar</button>
                </div>
      </form>
      <table class="table">
        <thead>
          <tr>
            <th scope="col">#</th>
            <th scope="col">Nome</th>
            <th scope="col">Sobrenome</th>
            <th scope="col">CPF</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <!--
            dados manuais...
            não esquecer de fazer as inserção dos dados do banco
            -->
            <th scope="row">1</th>
            <td>Mark</td>
            <td>Otto</td>
            <td>@mdo</td>
          </tr>
          <tr>
            <th scope="row">2</th>
            <td>Jacob</td>
            <td>Thornton</td>
            <td>@fat</td>
          </tr>
          <tr>
            <th scope="row">3</th>
            <td>Larry</td>
            <td>the Bird</td>
            <td>@twitter</td>
          </tr>
        </tbody>
      </table>
  </div>

 </main>

  <script type="text/javascript" src="./assets/js/painel.js"></script>
  <script type="text/javascript" src="./assets/js/jquery-3.4.1.min.js"></script>
  <script type="text/javascript" src="./assets/js/jquery.mask.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function(){
      $("#CPF").mask("000.000.000-00")
    })
  </script>

function Mudarestado(el) {
  document.getElementById(el).classList.toggle('mostrar');
}
#formulario {
    opacity: 0;
    transition: opacity 1.5s;
    border: 1px solid #ccf;
    padding: 1em;
    width: 78em;
    position: absolute;
}

#formulario.mostrar {
    opacity: 1;
}

#tabela {
    opacity: 0;
    transition: opacity 1.5s;
    border: 1px solid #ccf;
    padding: 1em;
    width: 78em;
    position: absolute;
}

#tabela.mostrar {
    opacity: 1;
}

  • Edit your question, are you using bootstrap? Which version? Cade your complete script to hide and hide the elements? And if you have created some extra css besides the original bootstrap put tbm the CSS that you made, without this can not answer you

1 answer

2


Guy your code had some problems, the main thing is you didn’t put the id="tabela" in his <table>, so it didn’t grab the CSS and the click function wasn’t working.

inserir a descrição da imagem aqui

The second "problem" is that both the table and the form have no color of background, then when one appears over the other you keep seeing this overlap of the elements. The most practical way to solve this is by putting a color of background the background color of the page, #fff.

Another tip, decrease the time of transition, 1.5s is very time consuming, so time consuming that the elements mix together during the transition, I recommend using 400ms at best, but in your code I changed only to 1.0s to improve a little this point, but then you put as you think best...

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Page Title</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.0/css/bootstrap.min.css" />
  <link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
<style>
    #formulario {
      opacity: 0;
      transition: opacity 1.0s;
      border: 1px solid #ccf;
      padding: 1em;
      width: 78em;
      position: absolute;
      background: #fff !important;
  }
  
  #formulario.mostrar {
      opacity: 1;
  }
  
  #tabela {
      opacity: 0;
      transition: opacity 1.0s;
      border: 1px solid #ccf;
      padding: 1em;
      width: 78em;
      position: absolute;
      background: #fff !important;
  }
  
  #tabela.mostrar {
      opacity: 1;
  }
</style>
</head>
<body>
  
  <main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
      <h1 class="h2">Clientes</h1>
    </div>
    <div id="container">
  
       <!--
        ########################################################################################################################################################### BUTTONS ###################################################################################################################################################################-->
  
        <button type="button" class="btn btn-success btn-mostrar" onclick="Mudarestado('formulario')">Novo</button>
        <button type="button"class="btn btn-secondary btn-mostrar" onclick="Mudarestado('tabela')">Listar</button> 
  
        <!--
        ########################################################################################################################################################### FORMULARIO ################################################################################################################################################################-->
  
        <form id="formulario" method="POST" action="cadastrar.php">
              <div class="form-row">
                  <div class="col">
                      <label for="inputNome">Nome</label>
                      <input type="text" class="form-control" id="Nome" placeholder="Primeiro Nome">
                  </div>
                  <div class="col">
                      <label for="inputSobrenome">Sobrenome</label>
                      <input type="text" class="form-control" id="Sobrenome" placeholder="Ultimo Nome">
                  </div>
              </div>
            <div>
              <br>
              <div class="form-check form-check-inline">
                <input class="form-check-input" type="radio" name="sexo" value="Masculino">
                <label class="form-check-label" for="Radio1">Masculino</label>
              </div>
              <div class="form-check form-check-inline">
                <input class="form-check-input" type="radio" name="sexo" value="Feminino">
                <label class="form-check-label" for="Radio2">Feminino</label>
                <br>
              </div>
            </div>
              <div class="form-row">
                  <div class="form-group col-md-2">
                      <label >CPF</label>
                      <input type="text" class="form-control" name="CPF" id="CPF" placeholder="000.000.000-00">
                  </div>
                <div class="form-group col-md-">
                  <label for="inputAddress">Endereço</label>
                  <input type="text" class="form-control" id="Endereço" placeholder="RAU-SC">
              </div>
              </div>
                  <div class="input-group-prepend" style="display:inline-block; vertical-align: middle;">
                    <button class="btn btn-outline-success" type="button">Savar</button>
                  <button class="btn btn-outline-secondary" type="button">Cancelar</button>
                  </div>
        </form>

        <table class="table" id="tabela">
          <thead>
            <tr>
              <th scope="col">#</th>
              <th scope="col">Nome</th>
              <th scope="col">Sobrenome</th>
              <th scope="col">CPF</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <!--
              dados manuais...
              não esquecer de fazer as inserção dos dados do banco
              -->
              <th scope="row">1</th>
              <td>Mark</td>
              <td>Otto</td>
              <td>@mdo</td>
            </tr>
            <tr>
              <th scope="row">2</th>
              <td>Jacob</td>
              <td>Thornton</td>
              <td>@fat</td>
            </tr>
            <tr>
              <th scope="row">3</th>
              <td>Larry</td>
              <td>the Bird</td>
              <td>@twitter</td>
            </tr>
          </tbody>
        </table>
    </div>
  
   </main>
  
    <script type="text/javascript" src="./assets/js/painel.js"></script>
    <script type="text/javascript" src="./assets/js/jquery-3.4.1.min.js"></script>
    <script type="text/javascript" src="./assets/js/jquery.mask.min.js"></script>
    <script type="text/javascript">
      //$(document).ready(function(){
        //$("#CPF").mask("000.000.000-00")
      //})
      function Mudarestado(el) {
        document.getElementById(el).classList.toggle('mostrar');
      }
    </script>

  
  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</body>
</html>

  • 1

    ok friend worked perfectly, thank you

  • @Cool emersonfreitas that worked, tmj

Browser other questions tagged

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