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

javascript click button by id
how to call javascript function on button click in html
button onclick jquery
how to call controller method from onclick javascript button
how to call javascript function on button click in javascript
html button onclick link
jquery onclick call function
change button color onclick css

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>

So you have 2 problems here. One is the below code is being run right on load:

var result = generateQuote(quotesArray); 

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

This is causing the render of the quote on page load. You should instead wrap it in a function to prevent that. For instance you can write something like this:

function generateQuote(array) {
    var quoteIndex = Math.floor(Math.random() * quotesArray.length);
    var randomQuote;

    for (var i = 0; i < array.length; i++) { 
        randomQuote = array[quoteIndex]; 
    }

    return randomQuote;
}

function renderQuote() {
    var result = generateQuote(quotesArray); 

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

Then on button click, you can bind the renderQuote:

button.addEventListener('click', renderQuote);

Using an HTML button to call a JavaScript function, When the user clicks a button, that clicks to is an event. Other examples include events like pressing any key, closing a window, resizing a� It specifies the function to run when the event occurs. map: It specifies an event map ({event:func(), event:func(), …}) having one or more event to add to the selected elements, and functions to run when the events happens. jQuery click() Method: This method triggers the click event, or adds a function to run when a click event occurs. Click event occurs when an element is clicked.


You also need to pass the quotesArray as a parameter to the function

So replace

button.addEventListener('click', generateQuote);

with

button.addEventListener('click', () => generateQuote(quotesArray));

How to call a JavaScript function on a click event?, The onClick event is the most frequently used event type, which occurs when a user clicks the left button of the mouse. You can put your� 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


create a function and insert everything. and create button with onclick option.

<input type="button"  onclick="generateQuote()>

How to call a JavaScript function from an onClick event?, A function is a block of code that runs when we tell it to. In this example, we will show how to run a function when a button is clicked. The first� Calling a PHP function using the HTML button: Create an HTML form document which contains the HTML button. When the button is clicked the method POST is called. The POST method describes how to send data to the server. After clicking the button, the array_key_exists() function called. Program 1:


On the second parameter you want to call a fonction reference, not the function itself. So you can do the following:

 button.addEventListener('click', function(quotesArray){
   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;
});

How to run a function with a button click in JavaScript, Get code examples like "how to call a function when a button is clicked in javascript" instantly right from your google search results with the Grepper Chrome� Obviously, when you just click (left) on start menu button then you’ll get the new “start menu“!! I still agree easiest way to launch run option is to press Win-key+R . Apart from RUN option, you’ll be able to see loads of other useful options like :- All the following options are available when you right click on the start menu button


Wrap your call around a function block and pass appropriate parameters

  button.addEventListener('click', function (){generateQuote(quotesArray)});

how to call a function when a button is clicked in javascript Code , (back to html) <input type="button" value="click me" onclick="myFunction()" /> if the button is clicked, it will execute the function. made on jsbin quickly: http://jsbin. Using onclick to create a dropdown button: // Get the button, and when the user clicks on it, execute myFunction document.getElementById("myBtn").onclick = function() {myFunction ()}; /* myFunction toggles between adding and removing the show class, which is used to hide and show the dropdown content */


How to make your JS work when a button is clicked, The onclick event is used to call a function when an element is clicked. That is why it is mostly used with the JavaScript function. Let's consider an example where� if you really need to. There are 2 things you can do 1 create a function class and pass the attributes to the function: In python a function is basically just an object with the method __call__ note the double underscore signifying its a special method. When a function gets called its __call__ which will get called so you could write something


How to Make Button onclick in HTML, When the button is clicked the method POST is called. The POST method describes how to send data to the server. After clicking the button, the� In our button, we have added our function to the command just like you can see below: button = Button(win, text="Click me", command = my_fun) button.pack() Here we have to pass our function name that we want to bind with our button. The function we have passed to our command will hold all the code that we want to run when our button will be


How to call PHP function on the click of a Button ?, JavaScript button click and JavaScript click event explained with code and indicate the onclick function in JavaScript you wish to execute. I have a Bootstrap navtab panel that when the tabs are clicked, based on which tab is clicked it runs an MVC C# function in my controller. I actually need this to happen on a button click. SO the user enters a date into the datepicker, clicks submit, and then based on which tab is selected, a function will be run.