The issue with Django's JavaScript functionality is that the dollar sign is not recognized and

Recently, I stumbled upon a fantastic chat feature in a Django project using version 2.0. The creator shared their work on Utopian here along with the git repository here. I tried to recreate the project following the given guide but faced challenges when integrating it into my existing app.

My current application utilizes Bootstrap and JavaScript for various functionalities, such as adding items to the cart. However, when adding the new chat feature, I encountered issues. The existing JavaScript functions properly, but the new one doesn't. I received a common error:

Uncaught ReferenceError: $ is not defined

Research on this error pointed out that something is being used before it is defined. Despite having experience in Python, I am a novice in JavaScript and struggling to identify the problem. Upon further inspection of the error, I found the following code snippet:

    <script src="https://project.s3.amazonaws.com/static/js/chat.js"></script>
<script>
    // For receiving
    sender_id = "";
    receiver_id = "1";

    //For sending
    $(function () {      <<--  This is where the error is pointing to.
        scrolltoend();
        $('#chat-box').on('submit', function (event) {
            event.preventDefault();
            var message = $('#id_message');
            send('', '', message.val());
            message.val('');
        })
    })
</script>

</div>

The above code snippet is located at the bottom of an HTML file within the project's directory and references the chat.js file using {% static 'js/chat.js' %}. The new integration includes scripts like 'materialize.js' and 'materialize.min.js', which I am still trying to comprehend.

I suspect that I may be mixing different types of JavaScript, leading to confusion. I am currently at a loss on how to proceed. Any advice or guidance from experienced developers would be greatly appreciated. If additional code snippets are needed for further analysis, please let me know.

Thank you.

Answer №1

To proceed, you need to include the following code snippet

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

right before

<script src="https://project.s3.amazonaws.com/static/js/chat.js"></script>

the specified line

Answer №2

It appears the variables have not been properly declared.

Here is the correct way to declare the variables:

var sender_id = "";
var receiver_id = "1";

// For ES6, you can use:

const sender_id="";
const receiver_id = "1";

In addition, consider including jQuery as suggested by Ahmet Zeybek.

Materialize.js and Materialize.css are part of the Materialize framework, similar to Bootstrap.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transmitting data from the backend to AngularJS during page load

After diving into Angular JS for the first time, I've hit a roadblock at an essential stage. My setup consists of an AngularJS front end and Grails backend. Check out the code snippets below along with my query. URL Mapping entry: as "/graph"(cont ...

I could use some assistance with deciphering JSON data

After using console.log to display the data I received, I observed an object structured as follows (I trimmed some details for clarity and used ... to indicate repetitive information): [ Submission { title: 'Untitled', content: { ur ...

Invoke data-id when the ajax call is successful

Initially, there was a smoothly working "like button" with the following appearance: <a href="javascript:void();" class="like" id="<?php echo $row['id']; ?>">Like <span><?php echo likes($row['id']); ?></span ...

Is add1 missing from the DOM? Learn how to include it

Having an issue with Java-Script and an HTML form. The scenario is that there's a main form with an "Add" button, which when clicked should display a second form. Next to the second form is another button labeled "Add1," which ideally should trigger ...

javascript for each and every textarea

I am looking to apply my JavaScript code to all the Textarea elements on my page. $_PAGE->addJSOnLoad(" $('.textarea').each(function() { $(this).keyup(function() { var characterCount = $(this).val().length; var mes ...

Keep only certain fields and eliminate the rest

Write a function that filters out all fields except 'firstName' and 'lastName' from the objects. Check out this code snippet I came up with. Any feedback? let people = [ { firstName: 'John', lastName: &apo ...

Page Not Found: Troubleshooting Express Server Issue

Currently, I am working on creating a basic parser using Node/Express and Cheerio. However, even though the server is running smoothly, I am unable to load any pages in my browser. Below is the code snippet from server.js: var express = require('expr ...

Error: Failed to parse the given color specification

My chrome extension is showing an error message: Unchecked runtime.lastError: The color specification could not be parsed. This error seems to be in the popup.html: 1 -> <! DOCTYPE html> line. Can anyone explain what this means and how to fix ...

How to assign attributes to all child elements in Angular?

I have a unique component in Angular that I utilize throughout my app. It's a button component which I use by calling <app-delete-btn></app-delete-btn> wherever needed. I tried to set the tabindex="1" attribute for my component ...

Extract a value from an array that is not located at the array's tail using Javascript

In my array pvalue, each number is unique. It contains: 1 2 3 15 20 12 14 18 7 8 (total of 10 numbers). My goal is to remove the number "15" from the array, resulting in pvalue being: 1 2 3 20 12 14 18 7 8 (now with 9 numbers). How can I achieve this with ...

Issue: [Error code 71] A protocol error occurred with pyvenv

Currently, I am using Centos7 with Vagrant and VirtualBox on Windows 10 for my development environment. My goal is to create a pyvenv virtual environment in order to work on Python web applications with Django. Keep in mind, I have already installed Python ...

Ways to break down a collection of multiple arrays

Looking to transform an array that consists of multiple arrays into a format suitable for an external API. For example: [ [44.5,43.2,45.1] , [42, 41.2, 48.1] ] transforming into [ [44.5,42], [43.2,41.2] , [45.1, 48.1] ] My current code attempts this ...

The Chartjs bar graph fails to display data upon initial page load

My HTML page includes a bar chart created using the ChartJS library. However, upon loading the page, the chart appears empty without displaying the data I provided and without rendering the bars. It only responds after clicking on the legend - first displa ...

SystemJS is loading classes that are extending others

In my Angular2 application, I have two classes where one extends the other. The first class is defined in the file course.ts (loaded as js) export class Course { id:string; } The second class is in schoolCourse.ts (also loaded as js) import {Cours ...

The Angular Universal error arises due to a ReferenceError which indicates that the MouseEvent is not

I am encountering an error while trying to utilize Angular Universal for server-side rendering with the command npm run build:ssr && npm run serve:ssr. This is being done in Angular8. /home/xyz/projects/my-app/dist/server/main.js:139925 Object(tslib__WEB ...

Unable to execute the command: python manage.py runserver

When I try to run the command 'python manage.py runserver' on my terminal, it doesn't seem to work. Here is what the terminal outputs: image preview Could someone please assist me with this issue? I attempted to start the Django developme ...

Choose the span element within every other td that is not enclosed by an anchor tag

The table structure I am working with is as follows: <tr> <td> <a> <span> some content </span> </a> </td> <!-- td having straight span --> <td> <span> ...

Shared variables in Node.js allow for multiple users to access different entry points simultaneously

In the process of transitioning my node-js application from a single-tenant database to a multi-tenant database, I am facing some challenges. The application code is accessed through an express api and various services are run through different entrypoints ...

Saving asp.net strings in different formats to datetime fields in a database

Can someone please assist me with saving the string value of textbox to the database as a datetime, and for updating/editing purposes, display it again in the textbox as a string? The reason for this is that the datetime input will be copied from Outlook a ...

Unable to bind knockout dropdownlist data during an ajax request

Trying to connect a dropdownlist in knockout with MVC 4. Below is the code snippet: Action public JsonResult GetUserTypes() { using (QuestApplicationEntities db = new QuestApplicationEntities()) { var usertypes = (from ...