How to create a panel that Displays/Hides fields using Html/Bootstap/Javascript

Asked

Viewed 1,031 times

1

I need to implement on a screen, a search panel that needs to appear when I click the button (Advanced Search) and within that panel there should be a "Close" button that, when clicked, will hide the same... How do I do it?

//Hidden Panel inserir a descrição da imagem aqui

//Visible panel inserir a descrição da imagem aqui

//Button code

<button id="btnPesquisaAvancada" type="button" class="btn btn-outline btn-default" data-toggle="tooltip"
		data-original-title="Pesquisa Avançada" data-container="body">
	<i class="icon wb-search" aria-hidden="true"></i>
	<span class="hidden-xs">Pesquisa Avançada</span>
</button>

2 answers

1


One of the ways to do it is by using jQuery in a simple way with the method toggle.

$(document).ready(function() {
  
   $(".divOculta").hide();
  
   $("#btnPesquisaAvancada").on("click", function() {
      $(".divOculta").toggle();
   })
  
})
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button id="btnPesquisaAvancada" type="button" class="btn btn-outline btn-default" data-toggle="tooltip"
		data-original-title="Pesquisa Avançada" data-container="body">
	<i class="icon wb-search" aria-hidden="true"></i>
	<span class="hidden-xs">Pesquisa Avançada</span>
</button>

<br><br>

<div class=" col-12 divOculta">
  <div class="form-group col-sm-6">
    <label for="sit">Situação</label>
    <input type="text" class="form-control" id="sit" placeholder="Enter email">
  </div>
  <div class="form-group col-sm-6">
    <label for="cad">Tipo de cadastro</label>
    <input type="text" class="form-control" id="cad" placeholder="Enter email">
  </div>
</div>

  • Thanks for the help @Leandro!!!

  • For nothing man! :)

0

I made an option with jQuery that can meet you. It makes a .slideToggle() in the advanced search form. As it seems that you are using BS4 I put the css of this framework, but it works in any environment that has jQuery

$("button").click(function(){
    $(this).toggleClass("ativo");
    $(".busca").slideToggle();
});
.busca{
    display: none;
 }
 .ativo {
     background-color: royalblue;
 }
<link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />
    
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

     
         <button id="btnPesquisaAvancada" type="button" class="btn btn-outline btn-default" data-toggle="tooltip"
                 data-original-title="Pesquisa Avançada" data-container="body">
             <i class="icon wb-search" aria-hidden="true"></i>
             <span class="hidden-xs">Pesquisa Avançada</span>
         </button>
     
        <form class="busca">
            <div class="form-row">
                <div class="col">
                <input type="text" class="form-control" placeholder="First name">
                </div>
                <div class="col">
                <input type="text" class="form-control" placeholder="Last name">
                </div>
            </div>
        </form>
           

  • Thanks for the help @hugocsl... The panel is appearing open. How do I keep it always closed and appear only when clicked on the Advanced Search button?

  • @Jalberromano notices that he has a CSS that does the display:none in the element that will contain the "advanced search" form inside. So just vc put a class .busca{ display: none; } for example in the element that has the form inside and then use the script to toggle type $(".busca").slideToggle(); Notice that I edited my answer to get better understanding.

  • @Jalberromano summing up, then the . search already starts with the display:One and then you do the toggle with jQuery. Includes in the . css the rule to use to hide and then reference the script when clicking on btn. But when in doubt just say I’ll give you a boost if you need any further explanation.

  • Ah Simmmm, I hadn’t noticed that in css was the "display: None;"....

  • @Jalberromano yes you have to use to hide in the beginning, good luck with the project!

Browser other questions tagged

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