29

I'm not sure why but all the modal functions are not working with me. I checked the version and the load they are fine.

I keep getting this error message:

Uncaught TypeError: $(...).modal is not a function

for the hide I already found an alternative. instead of:

$('#myModal').modal('hide');

I used this :

$('#myModal .close').click();

And it works perfectly.

The problem now is with the show

$('#myModal').modal("show");

I also tried both

$('#myModal').modal("toggle");

And:

$('#myModal').modal();

but unfortunately none of them is working.

Here html code -when the button is clicked I will do some verification and handle a json data from the web service if it succeed then I want show my modal- everything is working except the show.

 <button type="button" id="creatNewAcount" class="btn btn-default" data-toggle="modal">Sign up</button>

if there's any alternative I would like to know it.

Nysa
  • 425
  • 1
  • 5
  • 14
  • 1
    You need to load jQuery first then jQuery UI/Bootstrap modal afterwards. Are you sure it's being loaded in that order? – MortenMoulder Apr 17 '16 at 03:20
  • 1
    are not they ?! ` ` – Nysa Apr 17 '16 at 03:33
  • It is not possible to answer unless you put your code – brk Apr 17 '16 at 03:42
  • Try a newer version of jQuery. 1.12 is quite old. – MortenMoulder Apr 17 '16 at 03:48
  • I'm sure, because as I mentioned the same thing happened with the hide but when I replaced it with the one that I have found It worked so I'm hoping for a replacement for the show. – Nysa Apr 17 '16 at 04:09
  • Probably you are woring on WP. About jQuery 1.12 and WP issue you can check out this: https://wordpress.stackexchange.com/questions/244537/why-does-wordpress-use-outdated-jquery-v1-12-4 – u.unver34 Nov 12 '17 at 03:31

18 Answers18

61

Most common reason for such problems are

1.If you have defined the jquery library more than once.

<script type="text/javascript" src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

    <div class="modal fade" id="myModal" aria-hidden="true">
    ...
    ...
    </div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

The bootstrap library gets applied to the first jquery library but when you reload the jquery library, the original bootstrap load is lost(Modal function is in bootstrap).

2.Please wrap your JavaScript code inside

$(document).ready(function(){});

3.Please check if the id of the modal is same

as the one you have defined for the component(Also check for duplication's of same id ).

Prakash Thete
  • 3,802
  • 28
  • 28
30
<div class="modal fade" id="myModal" aria-hidden="true">
   ...
   ...
</div>

Note: Remove fade class from the div and enjoy it should be worked


Note 2: If removing the fade class seems to work for you. This probably means that you have a version mismatch between your Bootstrap js and CSS.

You might be using a slightly newer version of Bootstrap js with a slightly older version of Bootstrap CSS. It can be easy to forget to upgrade the Bootstrap css when upgrading the Bootstrap js.

Updating the Bootstrap CSS to match the Bootstrap js should fix this issue.

Radhe Shyam sharma
  • 900
  • 15
  • 21
  • 2
    Could ypou please share the reason behind removing fade class.It was working with fade class in all most system but facing and issue on one perticular system and tried after removing fade class and get working. – Alfiza malek Jul 08 '20 at 09:55
  • @Rafikmalek I believe it is due to having another `fade` class somewhere overriding the fade class here. Removing should it just remove the special effects while keeping the functionality. – Jerry Jan 17 '21 at 06:19
  • 1
    You save my day, I read your answer and it helped me fixing a issue on production with real user on call :D – Iqbal Khan Aug 25 '21 at 06:30
  • This fixed my issue thanks! – digiwebguy Jan 26 '22 at 09:39
  • Thanks for this solution. However, the most common reason why removing the 'fade' works, is that you have added jQuery more than once. I would suggest removing the duplicate inclusion of jQuery rather than removing the 'fade' class. – Mujib Khan Sep 01 '22 at 12:02
5

This can happen for a few reasons:

  1. You forgot to include Jquery library in the document
  2. You included the Jquery library more than once in the document
  3. You forgot to include bootstrap.js library in the document
  4. The versions of your Javascript and Bootstrap does not match. Refer bootstrap documentation to make sure the versions are matching
  5. The modal <div> is missing the modal class
  6. The id of the modal <div> is incorrect. (eg: you wrongly prepended # in the id of the <div>)
  7. # sign is missing in the Jquery selector
Mohammed Shareef C
  • 3,829
  • 25
  • 35
3

I got same issue while working with Modal Popup Bootstrap , I used Id and trigger click event for showing and hidding modal popup instead of $("#Id").modal('show') and $("#id").modal('hide'), `

    <button type="button" id="btnPurchaseClose" class="close" data dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span></button>
<a class="btn btn-default" id="btnOpenPurchasePopup" data-toggle="modal" data target="#newPurchasePopup">Select</a>
    $('#btnPurchaseClose').trigger('click');// for close popup

     $('#btnOpenPurchase').trigger('click');`// for open popup
Muhammad Bilal
  • 1,008
  • 13
  • 14
2

My root cause was that I forgot to add the # before the id name. Lame but true.

From

$('scheduleModal').modal('show');

To

$('#scheduleModal').modal('show');

For your reference, the code sequence that works for me is

<script>
function scheduleRequest() {
        $('#scheduleModal').modal('show');
    }
</script>
<script src="<c:url value="/resources/bootstrap/js/bootstrap.min.js"/>">
</script>
<script
    src="<c:url value="/resources/plugins/fastclick/fastclick.min.js"/>">
</script>
<script
    src="<c:url value="/resources/plugins/slimScroll/jquery.slimscroll.min.js"/>">
</script>
<script
    src="<c:url value="/resources/plugins/datatables/jquery.dataTables.min.js"/>">  
</script>
<script
    src="<c:url value="/resources/plugins/datatables/dataTables.bootstrap.min.js"/>">
</script>
<script src="<c:url value="/resources/dist/js/demo.js"/>">
</script>
Siddharth
  • 9,349
  • 16
  • 86
  • 148
2

Use .modal({show:true}) and .modal({show:false}) instead of ('show') and ('hide') ... it seems to be bootstrap / jquery version combination dependent. Hope it helps.

LordBaconPants
  • 1,404
  • 1
  • 19
  • 22
2

the solution of 'bootstrap modal is not opening' is, put your Jquery CDN first in the head tag (after starting head tag).

I wasted two days in finding this problem.

  • Not sure why this is upvoted but an [earlier answer](https://stackoverflow.com/a/44047904/215552) saying the same thing is not. – Heretic Monkey Feb 05 '21 at 13:46
1

Please,

try and check if the triggering button has attribute type="button". Working example:

  <button type="button" id="trigger" style="visibility:visible;" onclick="modal_btn_click()">Trigger Modal Click</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog modal-sm">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <p>This is a small modal.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </div>

   <script >
           function modal_btn_click() {

               $('#myModal').modal({ show: true });
           }
   </script>
BR1COP
  • 321
  • 2
  • 5
0

Try This without paramater

$('#myModal').modal();

it should be worked

Ahmed Hany
  • 952
  • 6
  • 12
0

use the object to call...

<a href="#" onclick='$("#myModal").modal("show");'>Try This</a>

or if you using ajax to show that modal after get result, this is work for me...

$.ajax({ url: "YourUrl",
type: "POST", data: "x=1&y=2&z=3",
cache: false, success: function(result){
        // Your Function here
        $("#myModal").modal("show");
    }
});
Rhega
  • 104
  • 7
  • I don't want call it immediately. – Nysa Apr 17 '16 at 04:05
  • the question is... are you using ajax to do some verification and for any result you want to show it on modal popup ? – Rhega Apr 17 '16 at 04:20
  • I want to do that but not immediately I have some code and if this code generate the correct result then the modal should appear if not it will not appear. – Nysa Apr 17 '16 at 04:20
  • no I don't want to show the result in the modal. I want the modal to be shown in some cases depending on the result . – Nysa Apr 17 '16 at 04:27
  • I update my answer... or if still not working, can you show us that generate code ? – Rhega Apr 17 '16 at 04:31
  • thank you it is working now. it was a duplicate load. – Nysa Apr 17 '16 at 05:00
0

are you sure that the id of the modal is "myModal"? if not then the call will not trigger it.

also - just from reading your post - you are triggering a function / validation with this button

<button type="button" id="creatNewAcount" class="btn btn-default" data-toggle="modal">Sign up</button>

and then if all is well - you want to trigger the modal. - if this is hte case - then you should remove the toggle from this button click. I presume you have an event handler tied to this click? you need the .modal("show") as a part of that function. not toggled from this button. also - is this id correct "creatNewAcount" as opposed to this spelling "createNewAccount"

<button type="button" id="creatNewAcount" class="btn btn-default" >Sign up</button>
gavgrif
  • 15,194
  • 2
  • 25
  • 27
0
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script><script type="text/javascript" src="//code.jquery.com/jquery-1.11.3.min.js"></script>

the first googleapis script is not working now a days use this second scripe given by jquery

kumar
  • 3
  • 5
0

jQuery lib has to be loaded first. In my case, i was loading bootstrap lib first, also tried tag with target and firing a click trigger. But the main issue was - jquery has to be loaded first.

Sajal
  • 1,198
  • 1
  • 17
  • 32
0

Please also make sure that the modal div is nested inside your <body> element.

saar schweid
  • 91
  • 1
  • 1
0

After trying everything on the web for my issue, I needed to add in a small delay to the script before trying to load the box.

Even though I had put the line to load the box on the last possible line in the entire script. I just put a 500ms delay on it using

setTimeout(() => {  $('#modalID').modal('show'); }, 500);

Hope that helps someone in the future. 100% agree it's prob because I don't understand the flow of my scripts and load order. But this is the way I got around it

0

In my case, bootstrap.css and bootstrap.js versions are mismatched. It is working if I remove the fade class. But the background is disappeared. I added the same version of bootstrap files. Now it is working perfectly. Thank you @Mohammed Shareef C.

0

Another use case that may be the cuplrit

My problem was that I had a jquery selector that was using a variable that was not assigned immediately.

My modal had a dynamic ID

<div class="modal fade" id="{{scope.modalId}}"... >

This is in angularJS, so the valud of scope.modalID wasn't being bound until $scope.onInit

vm.$onInit = function () {
   $( `#${vm.modalId}` ).on('shown.bs.modal', function(){
     console.log('WORKS')
    });
zzz
Adam Hughes
  • 14,601
  • 12
  • 83
  • 122
0

Make sure your script tag is closed with another script tag like below

before

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"/>

After

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Sadique Khan
  • 230
  • 3
  • 9