Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda