Error: "$$" not defined in this context

I am currently working on generating a dynamic pie chart programmatically with the intention of transforming it into a reusable React Component. The main idea is to have an interactive pie chart where each slice expands into a full pie when clicked. I came across this tutorial on creating pie charts, and towards the end, there is some JS code that I attempted to implement. However, upon testing, I encountered an error message stating:

Uncaught Reference Error: $$ is not defined.
Here's a screenshot of the error for reference.

https://i.stack.imgur.com/0fonJ.png

My assumption is that this issue is related to the fact that the code does not utilize jQuery and simply relies on Vanilla JS. Despite importing jQuery through a CDN, the same error persisted. After perusing through this Stack Overflow post, my speculation is that $$ may just be a type of variable naming convention.

Presented below is the content of the index.html file, which doesn't contain any groundbreaking elements.

<body>
  <div class="pie">20%</div>
  <div class="pie">60%</div>
  <script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI=" crossorigin="anonymous"></script>
  <script type="text/javascript">
    $$('.pie').forEach(function(pie) {
      var p = parseFloat(pie.textContent);
      var NS = "http://www.w3.org/2000/svg";
      var svg = document.createElementNS(NS, "svg");
      var circle = document.createElementNS(NS, "circle");
      var title = document.createElementNS(NS, "title");
      circle.setAttribute("r", 16);
      circle.setAttribute("cx", 16);
      circle.setAttribute("cy", 16);
      circle.setAttribute("stroke-dasharray", p + " 100");
      svg.setAttribute("viewBox", "0 0 32 32");
      title.textContent = pie.textContent;
      pie.textContent = '';
      svg.appendChild(title);
      svg.appendChild(circle);
      pie.appendChild(svg);
    });
  </script>
</body>

What could be causing this error? Is there a misunderstanding on my part? Could the tutorial I'm following be outdated or incorrect? Thank you!

Answer №1

Here are the details you requested:

function $$(selector, container) {
  container = container || document;
  var elements = container.querySelectorAll(selector);
  return Array.prototype.slice.call(elements);
}

In her own words, Lea Verou explains:

Lea Verou mentions in her book that the introduction provides the definition of $$(), but this excerpt does not include it.

Answer №2

As mentioned in a helpful tutorial, I found something that may be useful in addressing your question.

https://i.stack.imgur.com/JJnrG.png

In addition, it is recommended to enclose your code within the following:

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

Given that you have referenced jQuery in your tags.

I hope this information proves helpful to you.

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

Is there a way to automatically initiate the download of a file (such as a PDF) when a webpage loads?

Currently, my objective is to have a form on a webpage that, once filled out by a user, redirects them to a thank you page where a message of gratitude is displayed. What I aim to accomplish is for a PDF file to automatically start downloading as soon as t ...

What is the reason for the successful addition of a list item, using dynamic methods, in Chrome and Firefox while encountering issues in

I have encountered an issue with my jQuery code that works perfectly fine in Firefox and Chrome but fails to work in Internet Explorer 8 (I haven't tested it on other versions of IE). It is important to note that Internet Explorer is running in standa ...

Searching for paired values within an array using Javascript or Jquery

In my JavaScript code, I am working with an array called ppts that looks like: var ppts = []; //... ppts.push({x: mouse.x, y: mouse.y}); //... var tmpArr = []; for (var i=1;ppts.length-1; i++) tmpArr.push(ppts[i].x); alert(tmpArr[2]); tmp_ctx.lineTo(pars ...

What alternative approach can be used to substitute initEvent in typescript?

I am interested in manually triggering a MouseEvent in typescript, but I have discovered that the initEvent method is deprecated. var clickEvent =document.createEvent('MouseEvent'); clickEvent.initEvent('mouseup',true,true); Are there ...

Revamp the Design Layout of a Drag-and-Drop Tool

After experimenting with a drag and drop feature using jQuery, I discovered the following useful code snippet. Here's how it functions: $( "#sortable1, #sortable2" ).sortable({ connectWith: ".connectedSortable" }).disableSelection(); #so ...

Image Switching Hover Bug

HTML: <div id="logo"></div> <div id="coming-soon"></div> JavaScript: $(document).ready(function(){ $("#logo").hover( function(){ $("#logo").fadeTo(300, 0); $("#coming-soon").fadeTo(300, 1.0 ...

Steps for dynamically loading the content of a Bootstrap 4 Modal

I am currently in the process of developing a website that will showcase a wide range of images. The design is set up as a landing page with all content contained within the main HTML page - there is only an index.html file for now. This website will serv ...

Securing logout functionality with jQuery and AJAX

Whenever a user logs in, an individual session id is created. There's a 'logout' link available on the webpage. By clicking this link, a jquery ajax function is triggered to run a php script that simply unsets the session id variable. If a ...

What is the process for dynamically inserting a new object into an array of data objects in Vue.js?

I am a beginner with vue.js and currently working on a form. I have an add button in my form, so when the user clicks on it, the same form field will be added to the form. Users can add as many times as they want. Here is my initial data: data () { ret ...

Trouble with HTTPS request in Android fragment

My app crashes and returns to the main activity whenever I try to use the search function with the Kitsu API in my fragment. I have observed through Logcat that no data is being fetched, but I am unable to determine what is causing the crash.The LogCat r ...

Leveraging the AngularJS model within a tabset framework

I am working with a tabset that has two options and I am binding data from a JSON file using Angular. What I would like to do is log the name of the tab that I click on to the console. I thought about using a "model" for this, but I am not sure if that is ...

Tips for extracting variables from a get OData call function

Is there a better approach to retrieving variables from a GET OData call? I'm struggling with extracting the 'id' variable from the call within my method. I've tried using callbacks, but have not been successful. Do you have any suggest ...

DNN 6.2.6 is experiencing issues with the functionality of jquery

In my latest project development, I am facing a challenge with creating a directory structure using JQuery. Despite trying to load jquery plugins in the same way as we do in regular web applications, it doesn't seem to work for me. Interestingly, when ...

ASP.NET master pages now come equipped with jQuery 1.10.2 already integrated for seamless functionality

I noticed that jQuery 1.10.2 is getting automatically added to my asp.net web forms master page, and I'm not sure why. Here is a screenshot for reference: https://i.sstatic.net/w9k7j.png When clicking on the script, it opens up the JS file even thou ...

Can next.js rewrites be configured with environment variables?

Currently, I am in the process of developing my application with a next.js frontend and express.js backend. During development, I simply run the relevant dev servers in the terminal. However, now I am looking to deploy my app using Docker for production. I ...

Ways to enhance the legibility and visibility of the text

On my website, there's an image that appears as follows: I attempted to add a shadow to the text, resulting in this look: However, the shadow seems out of place. Any ideas on how I can enhance readability without altering the text color or backgrou ...

Configuring a Custom Color for the Bootstrap Datetimepicker

Problem I encountered an issue with my projects that are both using the bootstrap-datetimepicker. While one project displays the correct color, as seen here, the other project shows a wrong color, shown here. Below is the HTML code snippet I am working wi ...

Utilizing jQuery to correspond with CSS media queries

Continuing from my previous question about an automatic jQuery image slider, you can refer to it here. I have made some changes in my CSS using media queries and I am trying to incorporate them into my JavaScript code using an 'if / else if' sta ...

The proper functioning of border-collapse and empty-cells together is not achieved

Refer to the link provided: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/Tables To conceal empty cells, use the rule empty-cells: hide;. This allows a cell's parent element background to shine through the cell if it is emp ...

How can I store the status of checked and unchecked checkboxes in an array of objects using Angular 7?

I have a set of checkboxes with a parent-child structure, and their values are generated dynamically in a loop. When I click the submit button, I want to capture the selected or unselected values in the specified format (as shown in the commented output) ...