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

179
Visualizações
How can i change html elements attributes with array

Hello i am trying to get this script to work but its just not working well the getquote function gets quote and a randome color from the defined arr and then update the page according to the the data it obtaines. after calling the getquote none of the ouput works as intended

Can anyone help show me where the fault is coming from or point out the broken part ??

var colors = [
  '#490A3D',
  '#BD1550',
  '#E97F02',
  '#F8CA00',
  '#8A9B0F',
  '#69D2E7',
  '#FA6900',
  '#16a085',
  '#27ae60',
  '#2c3e50',
  '#f39c12',
  '#e74c3c',
  '#9b59b6',
  '#FB6964',
  '#342224',
  '#472E32',
  '#77B1A9',
  '#73A857'
];

var quotes = [
  ["You only live once, but if you do it right, once is enough.", "Mae West"]
]

var currentQuote = "";
var currentAuthor = "";
var randomquote = "";
var randomcolor = "";

function getQuote() {
  randomquote = Math.floor(Math.random() * quotes.length);
  randomcolor = Math.floor(Math.random() * colors.length);
  currentQuote = quotes[randomquote][0];
  currentAuthor = quotes[randomquote][1];

  $(document).ready(function() {
    $('html body').animate({
      backgroundColor: colors[randomcolor],
      color: colors[randomcolor]
    }, 500);
    $('#newquote, .social-icons .fa-twitter').animate({
      backgroundColor: colors[randomcolor]
    }, 500);
    $('blockquote footer').animate({
      color: colors[randomcolor]
    }, 500);
    $('blockquote').animate({
      borderLeftColor: colors[randomcolor]
    }, 500);
    $('#quotetext').animate({
      opacity: 0
    }, 500, function() {
      $(this).animate({
        opacity: 1
      }, 500);
      $(this).text(currentQuote);
    });
    $('#quotesource').animate({
      opacity: 0
    }, 500, function() {
      $(this).animate({
        opacity: 1
      }, 500);
      $(this).text(currentAuthor);
    });
  });
}



getQuote();

$(document).ready(function() {
  $('#newquote').on('click', getQuote());

});
body {background:#16a085;}
.btn-primary {
    width: 100px;
    height: 40px;
    background:#490A3D;
    }
<body>
<h2 id="quotetext"></h2>
<footer id="quotesource"></footer>
<button class="btn btn-primary" id="newquote">New Quote</button>
</body>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

$(document).ready(function() {
  $('#newquote').on('click', getQuote);

});

You need to pass callback as a variable. In your example it is being invoked and the result of the function (which is nothing) is passed to event listener.

Working code on Codepen

about 4 years ago · Juan Pablo Isaza 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