Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Collapse Not Working with JQuery

This is driving me insane.

I have this:

<div class="container">
        <div class="panel panel-primary">
            <div class="panel-heading" id="panel-head">


                <a data-toggle="collapse" data-target="#collapseDiv" class="white-link" id="toggle" >Records Added
                    <span class="indicator glyphicon glyphicon-chevron-down pull-left pad-right" id ="glyphicon"></span>
                </a>

            </div>


            <div id="collapseDiv" class="panel-collapse collapse">
                <div class="panel-body">

And I am trying to fire the collapse with this (in various ways):

$('#collapseDiv').collapse("toggle")

All I get is:

Uncaught TypeError: $(...).collapse is not a function(…)

Any ideas?

```

```

like image 889
Mick Avatar asked Feb 24 '16 11:02

Mick


4 Answers

Make sure Jquery is included above Bootstrap, it works fine

$('#collapseDiv').collapse("toggle");
.white-link{color:#fff;}
<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" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<div class="container">
  <div class="panel panel-primary">
    <div class="panel-heading" id="panel-head">


      <a data-toggle="collapse" data-target="#collapseDiv" class="white-link" id="toggle">Records Added
                        <span class="indicator glyphicon glyphicon-chevron-down pull-left pad-right" id ="glyphicon"></span>
                    </a>

    </div>


    <div id="collapseDiv" class="panel-collapse collapse">
      <div class="panel-body">
like image 185
Luthando Ntsekwa Avatar answered Nov 19 '22 02:11

Luthando Ntsekwa


I was having the same problem.... So in JQuery I did something like this:

$('#collapseDiv').removeClass('show');

As mentioned in official doc...

  1. .collapse hides content
  2. .collapsing is applied during transitions
  3. collapse.show shows content

so that is why I removed 'show' class and it worked for me... only drawback is that it hides immediately (without any animation)

like image 35
Zohab Ali Avatar answered Nov 19 '22 00:11

Zohab Ali


I had a similar problem with Lightbox (by Lokesh Dhakar) if I used:

<script src="js/lightbox-plus-jquery.min.js"></script>

but worked with:

<script src="js/lightbox.min.js"></script>
like image 1
Ovidiu Luca Avatar answered Nov 19 '22 01:11

Ovidiu Luca


It took me an hour to figure it out.
Here is the simple solution. I will explain with three different method
1. Replace

import * as $ from 'jquery';

with

declare var $ : any;

If it did't solve your problem than
2. make sure you import jquery and bootstrap in index.html as below, jQuery shoud be first

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>      
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">   
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

If it did't work
3. than install jquery,jquery ui,jquery action and bootstrap

npm install jquery --save
npm install @types/jquery --save
npm install bootstrap --save

Now import in your ts or js file

import * as $ from 'jquery'; 

Update angular.json file

"styles": [
              "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
              "src/styles.scss",
              "./node_modules/font-awesome/css/font-awesome.css",
              "./node_modules/bootstrap/dist/css/bootstrap.min.css"

            ],
            "scripts": [
              "./node_modules/jquery/dist/jquery.min.js",
              "./node_modules/bootstrap/dist/js/bootstrap.min.js"
            ]

That all it should work by now,Thanks

like image 1
codingwithtashi Avatar answered Nov 19 '22 01:11

codingwithtashi