Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
Can't get button to connect to javascript code

I'm new to JavaScript and I was making a random quote generator but I can't seem to get my js code to link to the button. Is it something to do with my selectors?

let quotes = ['The greatest glory in living lies not in never falling, but in rising every time we fall.', 'The way to get started is to quit talking and begin doing.', 'If life were predictable it would cease to be life, and be without flavor.', 'Life is what happens when youre busy making other plans.', 'Spread love everywhere you go. Let no one ever come to you without leaving happier.'];
let author = ['Nelson Mandela', 'Walt Disney', 'Eleanor Roosevelt', 'John Lennon', 'Mother Teresa'];
const quoteId = document.querySelector('.quote');
const authorId = document.querySelector('.source');
const btn = document.getElementById('loadQuote');


btn.addEventListener('click', function() {
  quote.textContent = quotes[randomNum()];
  source.textContent = author[randomNum()];
});


function randomNum() {
  Math.floor(Math.random() * quotes.length)
};
<div id="quote-box">
  <p class="quote">You can do anything but not everything</p>
  <p class="source">David Allen<span class="citation">Making It All Work</span><span class="year">2009</span></p>
</div>
<button id="loadQuote">Show another quote</button>
</div>

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

There are different mistakes:

In named function you sould explicitly write "return" what you want to be returned by the functions,

Assuming "quote" and "author" are paired by their position, you should use a common "randNum" in the function passed in the event listener,

Other type error fixed.

let quotes = [
  "The greatest glory in living lies not in never falling, but in rising every time we fall.",
  "The way to get started is to quit talking and begin doing.",
  "If life were predictable it would cease to be life, and be without flavor.",
  "Life is what happens when youre busy making other plans.",
  "Spread love everywhere you go. Let no one ever come to you without leaving happier.",
];
let author = [
  "Nelson Mandela",
  "Walt Disney",
  "Eleanor Roosevelt",
  "John Lennon",
  "Mother Teresa",
];
const quoteId = document.querySelector(".quote");
const authorId = document.querySelector(".source");
const btn = document.getElementById("loadQuote");

btn.addEventListener("click", function () {
  let randNum = randomNum();
  quoteId.textContent = quotes[randNum];
  authorId.textContent = author[randNum];
});

function randomNum() {
  return Math.floor(Math.random() * quotes.length);
}

about 4 years ago · Santiago Gelvez Relatório

0

I've had this issue many times in the past. You can do one of two things:

You can change the btn.addEventListener('click', ...) to btn.onclick = function() { ... }

Or, you can change btn.addEventListener('click', ...) to btn.addEventListener('onclick')

This happens because the 'click' function is run since it is a query of the button element. Imagine it like it is inside the button element where you would do <button onclick="myFunction()"></button>.

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda