I'm coding a web app with Django Channels and WebSockets, and I want to dynamically create the bootstrap toasts (.2/ponents/toasts/) when a WebSocket connection is open. The problem is that an error is raised "Uncaught TypeError: $(...).toast is not a function". So the question is how to read the $('.toast').toast('show');
line in Socket.onopen
(I'm not really into jQuery). I'll be thankful if you have any ideas?
Here is a part of the js file:
var Socket = new ReconnectingWebSocket(ws_path)
Socket.onopen = function (e) {
newToast();
$('.toast').toast('show');
};
function newToast(data) {
// create Bootstrap toast element
};
In the head of html file, Bootstrap and jQuery are loaded:
<head>
<link rel="stylesheet" href=".3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src=".3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src=".3.1/jquery.min.js"></script>
<script src=".3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>
PS: I've already tried to replace the dollar sign with jQuery. Same error : Uncaught TypeError: jQuery(..) ...
I'm coding a web app with Django Channels and WebSockets, and I want to dynamically create the bootstrap toasts (https://getbootstrap./docs/4.2/ponents/toasts/) when a WebSocket connection is open. The problem is that an error is raised "Uncaught TypeError: $(...).toast is not a function". So the question is how to read the $('.toast').toast('show');
line in Socket.onopen
(I'm not really into jQuery). I'll be thankful if you have any ideas?
Here is a part of the js file:
var Socket = new ReconnectingWebSocket(ws_path)
Socket.onopen = function (e) {
newToast();
$('.toast').toast('show');
};
function newToast(data) {
// create Bootstrap toast element
};
In the head of html file, Bootstrap and jQuery are loaded:
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn./bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery./jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn./bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>
PS: I've already tried to replace the dollar sign with jQuery. Same error : Uncaught TypeError: jQuery(..) ...
Share Improve this question edited Jul 26, 2021 at 20:19 Federico Navarrete 3,2845 gold badges49 silver badges86 bronze badges asked Jan 4, 2020 at 16:50 YassineYassine 1991 gold badge1 silver badge11 bronze badges 15-
1
Sounds like
$
is assigned something else than jQuery or the bootstrap JS library is not loaded at the time. Tryconsole.log($)
and see what that gives you. – Kordonme Commented Jan 4, 2020 at 18:22 -
1
@Yassine That output looks like your
$
is aliased to jQuery correctly. Is yourbootstrap.js
loading? – thingEvery Commented Jan 4, 2020 at 18:46 -
1
It's been ages since I've used jQuery, but that looks like a the jQuery method. What if you try
console.log($('.toast'))
andconsole.log($('.toast').toast('show'))
. First one will returnlength: 1
if the element exists. Second one should give you an error if Boostrap js is not loaded. – Kordonme Commented Jan 4, 2020 at 18:48 -
1
Can you do other stuff to that
.toast
div? Maybe something like:$('.toast').css('border', '3px solid red');
If that doesn't work either, your script might be firing before the DOM is ready. – thingEvery Commented Jan 4, 2020 at 18:53 - 1 I updated my answer again. Check it out. – thingEvery Commented Jan 4, 2020 at 19:03
1 Answer
Reset to default 0I was facing the same issue... The only solution that I could find was using google's CDN
<script src="https://ajax.googleapis./ajax/libs/jquery/3.3.1/jquery.min.js"></script>