Having trouble with collapsing elements on JavaScript?

I am encountering an issue with the code provided below. It seems to be working fine in this JSFiddle link: http://jsfiddle.net/e6kaV/33/. However, I am receiving the following error message:
{
"message": "Uncaught ReferenceError: $ is not defined",
"filename": "",
"lineno": 54,
"colno": 13
}
Any assistance would be greatly appreciated as I have limited knowledge of how JavaScript or jQuery functions.

.pane-launcher{
    position:absolute;
    top: 0;
    width:80px;
    height:80px;
    display:block;
}
#rules {
    left:0px;
}
#scenarios {
    left:90px;
}
.pane{
    position:absolute;    
    left: 0;
    height:50px;
    display:none;
    opacity:1;
}
#rules-pane {
    top:80px;
    width:170px;
    background-color:yellow;
}

#scenarios-pane {
    top:80px;
    width:170px;
    background-color:blue;
}
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>TEST</title>
        <link rel="stylesheet" href="css.css">
    </head>
    <body>
        <div id="rules" class="pane-launcher"><img src="http://placehold.it/80x80"></div>
        <div id="rules-pane" class="pane">Model1</div>
        <div id="scenarios" class="pane-launcher"><img src="http://placehold.it/80x80"></div>
        <div id="scenarios-pane" class="pane">Model2<br><img src="http://placehold.it/170x20"></div>

        <script>
            $(".pane-launcher").click(function () {
// Set the effect type
    var effect = 'slide';
 
    // Set the options for the effect type chosen
    var options = { direction: 'up' };
 
    // Set the duration (default: 400 milliseconds)
    var duration = 700;
    $('.pane.active, #'+this.id+'-pane').toggle(effect, options, duration).toggleClass('active');
});</script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    </body>
</html>

Answer №1

Ensure jQuery is loaded before utilizing it:

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>TEST</title>
        <link rel="stylesheet" href="css.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    </head>
    <body>
        <div id="rules" class="pane-launcher"><img src="http://placehold.it/80x80"></div>
        <div id="rules-pane" class="pane">Model1</div>
        <div id="scenarios" class="pane-launcher"><img src="http://placehold.it/80x80"></div>
        <div id="scenarios-pane" class="pane">Model2<br><img src="http://placehold.it/170x20"></div>

    <script>
    $(".pane-launcher").click(function () {
      // Set the effect type
      var effect = 'slide';

      // Set the options for the effect type chosen
      var options = { direction: 'up' };

      // Set the duration (default: 400 milliseconds)
      var duration = 700;
      $('.pane.active, #'+this.id+'-pane').toggle(effect, options, duration).toggleClass('active');
    });
    </script>
    </body>
</html>

The script should be placed before any code that relies on $ (representing jQuery), ideally in the <head>.

Answer №2

To utilize jquery, you need to include it before you can use it in your code. An easy solution is to move the script tag for jquery from the bottom of the code and place it before other scripts.

Check out this pen for a working example: http://codepen.io/anon/pen/xOzjWg

 $(".pane-launcher").click(function () {
// Set the effect type
    var effect = 'slide';
 
    // Set the options for the effect type chosen
    var options = { direction: 'up' };
 
    // Set the duration (default: 400 milliseconds)
    var duration = 700;
    $('.pane.active, #'+this.id+'-pane').toggle(effect, options, duration).toggleClass('active');
});
.pane-launcher{
      position:absolute;
        top: 0;
        width:80px;
        height:80px;
        display:block;
    }
    #rules {
        left:0px;
    }
    #scenarios {
        left:90px;
    }
    .pane{
        position:absolute;    
        left: 0;
        height:50px;
        display:none;
        opacity:1;
    }
    #rules-pane {
        top:80px;
        width:170px;
        background-color:yellow;
    }

    #scenarios-pane {
        top:80px;
        width:170px;
        background-color:blue;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<div id="rules" class="pane-launcher"><img src="http://placehold.it/80x80"></div>
<div id="rules-pane" class="pane">Model1</div>
<div id="scenarios" class="pane-launcher"><img src="http://placehold.it/80x80"></div>
<div id="scenarios-pane" class="pane">Model2<br><img src="http://placehold.it/170x20"></div>

Answer №3

Thank you to everyone for the assistance. My issue got resolved after I added these three script links, which was a solution provided in the suggested related question.

        <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
        <script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

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

Sequenced jQuery promises with the ability to halt execution

Currently, I am in the process of writing API code that involves multiple layers of wrapping around $.ajax() calls. One crucial requirement is to allow users to cancel any request, especially if it is taking too long. Typically, canceling a request can b ...

Making sure that the height of the <section> element is equal to the height of the viewport when the page

Is there a way to dynamically adjust the height of a <section> element to match the viewport every time the page is reloaded? Once the height is set, it should remain fixed even if the user resizes the window. For example, if the viewport's hei ...

Postponing the activation of toggleClass in jQuery until the completion of a slide animation

Having some trouble with the jQuery delay() function. I'm using it to pause a toggleClass action until after a slideUp animation on one of my divs, but it doesn't seem to be working as expected. I want a bar with rounded corners that expands whe ...

Experiencing difficulties with mocha and expect while using Node.js for error handling

I'm in the process of developing a straightforward login module for Node. I've decided to take a Test-Driven Development (TDD) approach, but since I'm new to it, any suggestions or recommended resources would be greatly appreciated. My issu ...

Utilizing space with margin and padding in Bootstrap 5

I recently started experimenting with bootstrap 5, but I'm feeling quite disoriented. The layout I have created includes some blue elements (borrowed the sidebar from this source) and now I am attempting to replicate this design. https://i.sstatic.ne ...

Obtain the Ajax function to provide a result of either true or false

I'm struggling to make an Ajax function return a true or false value for validation purposes. The other function needs to receive this value to determine the next steps. I'm certain I've accomplished this before, but for some reason, it&apo ...

Utilizing jQuery functions within Vue components in Quasar Framework

I've recently started delving into web app development and I'm encountering some basic questions regarding jQuery and Vue that I can't seem to find answers to. I have an application built using the Quasar Framework which frequently involves ...

Can we prevent a component from being mounted every time it is rendered by its parent component?

Is it possible to render the Child component within the Father component without mounting it and resetting its states when the Father component is rendered? I attempted to use the useMemo hook to render the Child component, but it still mounts the compone ...

The table toggle feature seems to be malfunctioning in Safari, whereas it works perfectly in Chrome

My table includes a td element that serves as a toggle switch, transitioning between 3 states flawlessly in Chrome. However, I am facing issues with its functionality in Safari and seek assistance in rectifying the issue to ensure cross-browser compatibili ...

Select a date by clicking, then enter the data into the input field

My setup includes two PHP files: index.php and add.php. Here's my situation: when a user clicks on a day link in index.php, I want them to be redirected to add.php and have the clicked day automatically populated in the date input field. Sample code ...

Troubleshooting issue: Chrome bug causing JavaScript full height intro section to have incorrect padding and display issues

Trying to create a full-height intro section using basic JavaScript markup has been quite the challenge. Here's a more detailed explanation: I have a parent DIV element that is supposed to adjust to the full height of the viewport. Unfortunately, t ...

Guide to implementing bidirectional data binding for a particular element within a dynamic array with an automatically determined index

Imagine having a JavaScript dynamic array retrieved from a database: customers = [{'id':1, 'name':'John'},{'id':2, 'name':'Tim}, ...] Accompanied by input fields: <input type='text' na ...

Requesting an API token through the body using Javascript's Fetch function

I'm currently working on developing a frontend application using Javascript Fetch to interact with an API service. One of the tasks I need to accomplish is to create a token by using the POST method and sending an apiKey parameter in the Body. Once I ...

Include new options to the select box only if they are not already listed

I'm currently working on a situation where I need to transfer items from one select element to another, but only if they are not already in the second select: $('#srcSelect option:selected').appendTo('#dstSelect') My challenge is ...

Determining the exact position of an absolute element within a Bootstrap container

In my design, I am using a full-width cover image with a bootstrap container class containing an absolutely positioned image on top of the cover image. My goal is to have this image positioned exclusively on the right-hand side of the container. Below is ...

Selecting a Single Checkbox in a Group using jQuery

I am struggling to select a single checkbox among a group of checkboxes and cannot seem to find a solution. Below is the code I use to dynamically create my checkbox group. My goal is to be able to iterate through the group and choose individual checkboxe ...

What could be causing the error I encounter when attempting to install axios?

While attempting to incorporate axios into my project, I used the command below: npm install axios However, an error has been persistently popping up: npm ERR! Unexpected end of JSON input while parsing near '...devDependencies":{"co' ...

Execute a command and direct the input/output to designated files

My goal is to execute a child process in the background while also modifying its output (like adding timestamps) and directing it to specific files. Currently, I am approaching this task as follows: try { var out = fs.createWriteStream(`${this.logdir}/$ ...

Ajax request received no data from API

I have been facing an issue with my code where I am posting an email on an API URL using an HTML form and PHP code with an Ajax call. However, the response I am getting is empty. I need to display this response on the same page below the form. I am sharing ...

Using MongoMapper with Rails to efficiently render arrays in views

In my Rails application, I have a controller that retrieves data from MongoDB. The specific field I need is actually an array, and I want to display it in an erb view. Currently, my workaround involves setting the JavaScript variable directly in the view ...