Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67527

How do I get this function to run when a button is clicked?

$
0
0

I made a function that will show you a random quote whenever you press a button, the problem is that the function runs when the page is loaded. All the code works as it should but I can't find the reason why it runs immediately. I think the problem is that I call the function outside the function itself at var result = generateQuote(quotesArray);, but when I place it in the function itself it breaks.

I hope someone can help me find the problem, here is the code:

JavaScript:

const quotes = new Object();

const quotesArray = [{
    quote: "“Be yourself, everyone else is already taken.”",
    author: "- Oscar Wilde",
} , {
    quote: "“Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.”",
    author: "― Albert Einstein",
} , {
    quote: "“The important thing is not to stop questioning. Curiosity has its own reason for existing.”",
    author: "― Albert Einstein"
} , {
    quote: "“Expect everything, I always say, and the unexpected never happens.”",
    author: "― Norton Juster"
} , {
    quote: "“What lies behind you and what lies in front of you, pales in comparison to what lies inside of you.”",
    author: "― Ralph Waldo Emerson"
} , {
    quote: "“We are part of this universe; we are in this universe, but perhaps more important than both of those facts, is that the universe is in us.”",
    author: "―Neil deGrasse Tyson"
}]

    const button = document.querySelector('.generateQuote');
    const author = document.querySelector('#quoteAuthor');
    const quoteText = document.querySelector('#quote');

button.addEventListener('click', generateQuote);

function generateQuote(array) {
    var quoteIndex = Math.floor(Math.random() * quotesArray.length); 
    for (var i = 0; i < array.length; i++) { 
            var randomQuote = array[quoteIndex]; 
          } 
          return randomQuote; 
        }
        var result = generateQuote(quotesArray); 

        quoteText.innerHTML = result.quote;
        author.innerHTML = result.author;

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Quote Generator</title>
    <link rel="stylesheet" href="main.css">

</head>
<body>
    <div class="border">
    <div class="quoteContainer">
        <h2 class="title">Quote Generator</h2>
        <button class="generateQuote">Generate a inspiring quote</button>
        <blockquote id="quote"><h2>quote</h2></blockquote>
        <h3 id="quoteAuthor">Author of the quote</h3>
    </div></div>
    <script src="script.js"></script>
</body>
</html>

Viewing all articles
Browse latest Browse all 67527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>