0

I realise this question has been asked a few times but none of the solutions in previous questions have resolved my issue.

I am trying to hide a modal using: $('#modal').modal('hide')

but am getting: $(...).modal is not a function in the console.

However if I go into the developer console and enter: $('#modal').modal('hide') it works perfectly

I am getting jQuery/bootstrap via a CDN in the index.html and have checked the order of the imports and checked for multiple imports of jQuery.

EDIT: My jQuery/bootstrap scripts are being loaded at the top of the index.html and the JS module is included at the bottom of the page so the order seems correct. $('#modal').modal('hide') is being called on a $(window).on('resize') event

dellboyant
  • 27
  • 8
  • Could you add the relevant parts of your HTML to the question? – Zudo Dec 04 '18 at 13:16
  • 1
    This means that you load bootstrap js after you call `.modal()` method. Thus in a console you can use it, but not in your code. – Krzysztof Janiszewski Dec 04 '18 at 13:18
  • 1
    Possible duplicate of [Bootstrap modal: is not a function](https://stackoverflow.com/questions/25757968/bootstrap-modal-is-not-a-function) – Krzysztof Janiszewski Dec 04 '18 at 13:20
  • @KrzysztofJaniszewski my jQuery/bootstrap scripts are being loaded at the top of the index.html and the JS module is included at the bottom of the page so the order seems correct. `$('#modal').modal('hide')` is being called on a `$(window).on('resize')` event – dellboyant Dec 04 '18 at 14:13
  • Some frameworks rerender the page when the window is resized. Not sure if that's the case with bootstrap though, but if so that might cause the problem. – Mark Baijens Dec 04 '18 at 14:20

1 Answers1

2

If you enter in developer console and it works means, problem with document ready issue or javascript placement issue.

just try this to confirm.

$(function(){
   $('#modal').modal('hide');
});

If this working, then document ready issue.

another one is you should use this $('#modal').modal('hide'); after jquery-ui.js.

Please provide your code sample for further investigation.

Ramesh
  • 1,829
  • 2
  • 25
  • 30