Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collapsible bootstrap, show first element

I want to show the first element of this collapsible accordion. I did some research in internet but in vain

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha256-7s5uDGW3AHqw6xtJmNNtr+OBRJUlgkNJEo78P4b0yRw= sha512-nNo+yCHEyn0smMxSswnf/OnX6/KwJuZTlNZBjauKhTK0c+zT+q5JOCx0UFhXQ6rJR9jg6Es8gPuD2uZcYDLqSw==" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha256-KXn5puMvxCw+dAYznun+drMdG1IFl3agK0p/pqT9KAo= sha512-2e8qq0ETcfWRI4HJBzQiA3UoyFk6tbNyG+qSaIBZLyW9Xf3sWZHN/lxe9fTh1U45DpPf07yj94KsUHHWe4Yk1A==" crossorigin="anonymous"></script>

<div class="accordion" id="accordion2">
  <div class="accordion-group">
    <div class="accordion-heading">
      <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne" >
        Aller au dossier personnel d'un agent
      </a>
    </div>
    <div id="collapseOne" class="accordion-body collapse" style="height: 0px; ">
      <div class="accordion-inner">
        <form action="goToAgent.do" method="post">
          <p align="center">
            <input type="hidden" value="identite" name="retour">
            <label for="nomPrenom" >
              Entrer le nom complet de l'agent:
            </label>
            <input type="text" name="nomPrenom" class="typeahead" /> <br>
            <input type="submit" value="appliquer" class="btn btn-info" />
        </form>
      </div>
    </div>
  </div>
  <div class="accordion-group">
    <div class="accordion-heading">
      <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">
        Ajout d'un nouveau fonctionnaire
      </a>
    </div>
    <div id="collapseTwo" class="accordion-body collapse">
      <div class="accordion-inner">
        <!-- lorem -->
      </div>
    </div>
  </div>
</div>
like image 631
fatiDev Avatar asked Feb 13 '13 11:02

fatiDev


People also ask

How do I create a collapsible list in Bootstrap?

Just add data-toggle="collapse" and a data-target to the element to automatically assign control of one or more collapsible elements. The data-target attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse to the collapsible element.

How do you close one accordion when another opens?

How do you close one accordion when another opens? open accordian > at right side go to accordian seting > scrol down to Expand/Collapse Accordion Option On Page Load > choose Hide/close All Accordion..

How does Bootstrap collapse work?

The collapse JavaScript plugin is used to show and hide content. Buttons or anchors are used as triggers that are mapped to specific elements you toggle. Collapsing an element will animate the height from its current value to 0 .

How do you collapse an accordion by default?

This can be done by setting the 'active' property of jQuery Accordion as false.


2 Answers

You also can trigger a clic on the first collapse element

$("#accordion a:first").trigger("click");
like image 166
Adri Avatar answered Oct 28 '22 14:10

Adri


Update for v4 :

The class .show can be used on the .collapse element to open it.

<div class="accordion" id="accordion-parent">
    <a data-toggle="collapse" href="#collapseOne"></a> 
    <div id="collapseOne" class="collapse show" data-parent="#accordion-parent"></div>
    <a data-toggle="collapse" href="#collapseTwo"></a> 
    <div id="collapseTwo" class="collapse" data-parent="#accordion-parent"></div>
</div>

Here's the same example working with v4 : Demo v4 (jsfiddle)


Bootstrap v2

If you add the .in class to the .collapse, it stays open :

<div class="accordion-group">  
  <div class="accordion-heading">  
    <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">  
     Ajout d'un nouveau fonctionnaire
    </a>  
  </div>  
  <div id="collapseTwo" class="accordion-body collapse in"> 
    <div class="accordion-inner">
    </div>  
  </div>  
</div>

Demo (jsfiddle)


Here is the demo with a simplified markup : http://jsfiddle.net/Sherbrow/yYuRz/2/

You do need to remove the style="height: 0px;" from the .collapse element if you want it to be displayed.

like image 24
Sherbrow Avatar answered Oct 28 '22 14:10

Sherbrow