32

I am trying to create a collapsable component using Bootstrap. My code is this

<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

<div class="panel-group" id="accordion">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
      Collapsible Group Item #1
    </a>
      </h4>
    </div>
    <div id="collapseOne" class="panel-collapse collapse in">
      <div class="panel-body">
        Anim pariatur cliche reprehenderit
      </div>
    </div>
  </div>
</div>

The "Collapsible Group Item #1" when clicked should collapse the items in class "panel-body" but it doesn't there is no effect of click. I copied this code directly from Bootstrap documentation still it does not work. Can anyone figure out what is the problem?

Richard Parnaby-King
  • 13,858
  • 11
  • 64
  • 123
poshanniraula
  • 543
  • 1
  • 6
  • 14

10 Answers10

49

jQuery is required (if you're using Bootstrap 3 or 4) ;-)

<html>
<head>
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    <!-- THIS LINE -->
    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="panel-group" id="accordion">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h4 class="panel-title">
                <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
                    Collapsible Group Item #1
                </a>
            </h4>
        </div>
        <div id="collapseOne" class="panel-collapse collapse in">
            <div class="panel-body">
                Anim pariatur cliche reprehenderit
            </div>
        </div>
    </div>
</div>

</body>
</html>
nik7
  • 747
  • 2
  • 8
  • 17
MaiKaY
  • 3,797
  • 17
  • 25
  • 3
    Any idea why this function would not work with https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js ? – Claudio Jun 08 '17 at 18:01
20

bootstrap.js is using jquery library so you need to add jquery library before bootstrap js.

so please add it jquery library like

Note : Please maintain order of js file. html page use top to bottom approach for compilation

<head>
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
</head>
Jay Patel
  • 5,620
  • 1
  • 15
  • 19
10

Add jQuery and make sure only one link for jQuery cause more than one doesn't work...

Terabyte
  • 421
  • 2
  • 12
7

You need jQuery see bootstrap's basic template

a5hk
  • 6,552
  • 2
  • 22
  • 34
5

I was getting crazy for one hour... I reply to this question after 7 years, because if you are using the new Boostrap 5, the attributes are data-bs-toggle, data-bs-parent, and data-bs-target, pay attention to the -bs- in the middle.

ocirocir
  • 2,225
  • 1
  • 17
  • 30
1

If you are using latest Bootstrap version. Then you just need to replace this data-bs-toggle="collapse" data-bs-target="#navbarNav" to this data-toggle="collapse" data-target="#navbarNav" and don't need to do more. It means you just need to remove -bs-

0

I had this problem and the problem was bootstrap.js wasn't load in Yii2 framework.First check is jquery loaded in inspect and then check bootstrap.js is loaded?If you used any tooltip Popper.js is needed before bootsrap.js.

NaabNuts
  • 334
  • 3
  • 9
0

Make sure to change the bootstrap cdn versions to check. try bootstap4 . Avoid bootstrap 5 as it is still in beta

0

Another good reason for this is either incorrectly formatted html, so things aren't closed up properly, or your id tags have things like full stops in them, that jquery will bawk at.

netniV
  • 1,998
  • 1
  • 13
  • 22
-5

Maybe your mobile view port is not set. Add following meta tag inside <head></head> to allow menu to work on mobiles.

<meta name=viewport content="width=device-width, initial-scale=1">