# Problem with Random Quote Generator

**URL:** <https://forum.freecodecamp.org/t/problem-with-random-quote-generator/9446>\
**Category:** General\
**Created:** [June 9, 2016, 6:10pm UTC](https://forum.freecodecamp.org/t/problem-with-random-quote-generator/9446 "2016-06-09T18:10:51Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![RahulBaboota](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rahulbaboota/32/1290_2.png) [@RahulBaboota](https://forum.freecodecamp.org/u/RahulBaboota)\
**Post date:** [June 9, 2016, 6:10pm UTC](https://forum.freecodecamp.org/t/problem-with-random-quote-generator/9446/1 "2016-06-09T18:10:51Z")

</div>

Can anyone tell me what is wrong with my javascript code?

> `$(document).ready(function(){

> getQuote();

> $(‘button’).on(‘click’, function({  
> $getQuote();  
> }));  
> });  
> // Function which gets the quotes from forismatic api

> var getQuote = function()  
> {  
> $.ajax({

> ```
> url:'http://api.forismatic.com/api/1.0/?method=getQuote&format=jsonp&lang=en&jsonp=?',
> datatype: 'jsonp',
> 
> ```

> ```
> // If the request is succesful
> success: function(data)
> {
> $(.'quote-box').html(data.quoteText);
> 
> ```

> ```
> //Appending the authors name if any
> if (data.quoteAuthor != '')
> {
> $(.'author-name').html(data.quoteAuthor);
> }
> else
> {
> $(.'author-name').html("Unknown");
> }
> }
> 
> ```

> ```
> //To handle errors
> error: function(xhr, status, error)
> {
> $(.'quote-box').html("I'm sorry but there was an error with the API request");
> }
> 
> ```
> 
> });  
> };`

Here is the HTML file for the same

> ```
> <!DOCTYPE html>
> 
> ```

> ```
> <html>
> <head>
> 
> ```

> ```
> <title>Random Quote Generator</title>
> <!-- To ensure compatability with various devices !-->
> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
> <meta name="viewport" content="width=device-width">
> <!-- Including Amatic Google Font -->
> <link href='http://fonts.googleapis.com/css?family=Cabin' rel='stylesheet'>
> <!-- Bootstrap -->
> <link href="css/bootstrap.min.css" rel="stylesheet">
> <!-- Various stylesheets used -->
> <link rel="stylesheet" href=main.css>
> <!-- font-awesome for the heart !-->
> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.2/css/font-awesome.min.css">
> </head>
> 
> ```

> ```
> <body>
> 
> ```

> ```
> <div class="text-heading text-center container">
> Random Quote Generator
> </div>
> 
> ```

> ```
> <div class="quote-box container">
> </div>
> 
> ```

> ```
> <div class="author-name container">
> </div>
> 
> ```

> ```
> <div>
> <button class='btn paper btn-text container-fluid'>New Quote</button>
> </div>
> 
> ```

> ```
> <footer class="footer">
> <div class="container-fluid footer-text">
> Made with <i class="fa fa-heart" aria-hidden='true'></i> by Rahul Baboota
> </div>
> </footer>
> 
> ```

> ```
> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
> <!-- Include all compiled plugins (below), or include individual files as needed -->
> <script src="js/bootstrap.min.js"></script>
> <!-- Including main.js -->
> <script src='main.js'></script>
> 
> ```

> ```
> </body>
> 
> ```

> ```
> </html>
> 
> ```

---

<div class="post-metadata">

**Author:** ![BenGitter](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bengitter/32/12192_2.png) [@BenGitter](https://forum.freecodecamp.org/u/BenGitter)\
**Post date:** [June 9, 2016, 6:20pm UTC](https://forum.freecodecamp.org/t/problem-with-random-quote-generator/9446/2 "2016-06-09T18:20:46Z")

</div>

Hey, what is the problem, what doesn’t work or does it do nothing? One thing I noticed: where you set the onclick event, you placed a $ before the function name. I suppose that shouldn’t be there.

---

<div class="post-metadata">

**Author:** ![RahulBaboota](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rahulbaboota/32/1290_2.png) [@RahulBaboota](https://forum.freecodecamp.org/u/RahulBaboota)\
**Post date:** [June 9, 2016, 6:30pm UTC](https://forum.freecodecamp.org/t/problem-with-random-quote-generator/9446/3 "2016-06-09T18:30:15Z")

</div>

I removed it and the code still doesnt work. And the problem I’m having is that when i click the button nothin happens.

---

<div class="post-metadata">

**Author:** ![BenGitter](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bengitter/32/12192_2.png) [@BenGitter](https://forum.freecodecamp.org/u/BenGitter)\
**Post date:** [June 9, 2016, 6:34pm UTC](https://forum.freecodecamp.org/t/problem-with-random-quote-generator/9446/4 "2016-06-09T18:34:23Z")

</div>

I see your jQuery DOM selectors are wrong. You have `$(.'author-name')`, but that should be `$('.author-name')`. The dot of the class should also be between the quotes.

---

<div class="post-metadata">

**Author:** ![mtancoigne](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mtancoigne/32/159487_2.png) [@mtancoigne](https://forum.freecodecamp.org/u/mtancoigne)\
**Post date:** [June 9, 2016, 6:40pm UTC](https://forum.freecodecamp.org/t/problem-with-random-quote-generator/9446/5 "2016-06-09T18:40:28Z")

</div>

You may try to log what you get from server in the console for both success and fail cases (`console.log(data)`)… If the query is executed. Is it ?

---

<div class="post-metadata">

**Author:** ![RahulBaboota](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rahulbaboota/32/1290_2.png) [@RahulBaboota](https://forum.freecodecamp.org/u/RahulBaboota)\
**Post date:** [June 9, 2016, 6:42pm UTC](https://forum.freecodecamp.org/t/problem-with-random-quote-generator/9446/6 "2016-06-09T18:42:26Z")

</div>

I did that too but still it isnt working

---

<div class="post-metadata">

**Author:** ![mtancoigne](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mtancoigne/32/159487_2.png) [@mtancoigne](https://forum.freecodecamp.org/u/mtancoigne)\
**Post date:** [June 9, 2016, 6:46pm UTC](https://forum.freecodecamp.org/t/problem-with-random-quote-generator/9446/7 "2016-06-09T18:46:01Z")

</div>

Ok, so what are the error messages ? you’re too vague for us to help…

---

<div class="post-metadata">

**Author:** ![RahulBaboota](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rahulbaboota/32/1290_2.png) [@RahulBaboota](https://forum.freecodecamp.org/u/RahulBaboota)\
**Post date:** [June 9, 2016, 6:49pm UTC](https://forum.freecodecamp.org/t/problem-with-random-quote-generator/9446/8 "2016-06-09T18:49:41Z")

</div>

The main error is that when I click on my “New Quote” button … nothing happens.  
When I tried typing in getQuote() in my console…it showed an that getQuote is not defined.

---

<div class="post-metadata">

**Author:** ![BenGitter](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bengitter/32/12192_2.png) [@BenGitter](https://forum.freecodecamp.org/u/BenGitter)\
**Post date:** [June 9, 2016, 6:58pm UTC](https://forum.freecodecamp.org/t/problem-with-random-quote-generator/9446/9 "2016-06-09T18:58:46Z")

</div>

Have a look at [this](http://codepen.io/dting/pen/PqrZgb?editors=1010). Same API, so this should work.

---

<div class="post-metadata">

**Author:** ![tony-tyrone-taylor](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tony-tyrone-taylor/32/166029_2.png) [@tony-tyrone-taylor](https://forum.freecodecamp.org/u/tony-tyrone-taylor)\
**Post date:** [June 9, 2016, 7:18pm UTC](https://forum.freecodecamp.org/t/problem-with-random-quote-generator/9446/10 "2016-06-09T19:18:46Z")

</div>

From looking at your code I think you’re calling the function getQuote() before it’s declared!

if it was  
function getQuote(){

}

Then you can call it from anywhere in your script. Try placing var getQuote right to the top of the script. That’s the only thing I can see but I’m not an expert

Plus the other faults

$getQuote … not valid  
$(.’.class’) not valid

etc

---

<div class="post-metadata">

**Author:** ![RahulBaboota](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rahulbaboota/32/1290_2.png) [@RahulBaboota](https://forum.freecodecamp.org/u/RahulBaboota)\
**Post date:** [June 10, 2016, 5:27pm UTC](https://forum.freecodecamp.org/t/problem-with-random-quote-generator/9446/12 "2016-06-10T17:27:10Z")

</div>

I rectified all the mistakes pointed above and now when i click the button I get the error message. So I think there’s a problem with the API request?

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [January 16, 2021, 3:47am UTC](https://forum.freecodecamp.org/t/problem-with-random-quote-generator/9446/13 "2021-01-16T03:47:09Z")

</div>

Closed on January 16, 2021, 3:47am UTC


