$ is not defined

$ is not defined
0

#1

I have set a button with the id “new-quote”. When the button is pressed, a random quote is supposed to appear. When I press it, nothing happens. I pressed F12(Accessed my Developer tools) to see what was wrong. When I pressed the tab that says console, it has an error stating, “Uncaught ReferenceError: $ is not defined pen.js:1”.

Did I do anything wrong in my code? If so, what did I do?

If it’s something that has nothing to do with my code, can someone tell me how to solve this problem?

Thanks in advance


#2

You need to include jQuery. Here’s how.


#3

You cannot be serious. I have been stuck on this for days and I forgot to add jQuery. Thanks a lot I really appreciate it. Do you get brownie points if I say thanks on the forum?


#4

Nope. Don’t worry, though. I have actual brownies!


#5

It’s 9am where I’m at and I have had no breakfast. Reading your comment made my stomach growl so loud

Here’s my new code because the API didn’t work like I thought it would. Now nothing’s happening. Can you or anyone please tell me what’s wrong while I get some cereal?


#6

Yus.

First, you don’t need the forEach() call to access one element in an array. You know you’re getting just one quote object, and you know where that object will be (index 0), so you can access it directly.

$("#quote").html("<em>" + data[0].content + "<br>" + "<p>" + data[0].title + "</p>" + "</em>");

No need to call any methods on or iterate through the array! Incidentally, I think you’ve got forEach() conceptually wrong, but you’re very close. Think of it like an automated for loop, where key is the same thing as data[i]. So, data[key] is like trying to get data[data[i]]. Also, you don’t need to worry about incrementing, which is what I believe you were trying to do with key++.


#7

Don’t worry, I had a similar issue, was stuck on portfolio for days because my phone had an old phone specific issue while I thought the chrome dev tools weren’t precise enough, and so I searched everywhere for what went wrong, turns out it was just an old phone issue… And chrome dev tools work.


#8

I spent days trying to figure out why changes I made to code weren’t working, and then I realized I had an old file in my browser… I think everyone has a brain fart at sometime with computers.