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

168
Vistas
Back and Forth Buttons to modify an htl template with new strings

I have to write down a code that allows me to change an attribute of an element. I want to do this through buttons. Basically, the forward button has to choose the next element of a javascript array. The backward button, instead, the previous one. I tried something like this with poor results:

<!--Let us suppose that we have a paragraph-->
<p id='par' style='color:green'>Something</p>
<button onclick="forward()">TGo On !</button>
<!-- The backward button will follow a similar logic-->
<script>
  function forward() {
    const colors = ['color:pink', 'color': green ','
      color: blue ','
      color: red '];
      if (colors.indexOf(document.getElementById('par').getAttribute('style')) < colors.length)
        document.getElementById('par').setAttribute('style', colors[colors.indexOf(document.getElementById('par').getAttribute('style')) + 1])
      else {}
    }
</script>

Thank you in advance

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use the style property instead of the HTML attribute to edit styles via javascript.

function forward() {
  const paragraph = document.getElementById('par');
  const colors = ['pink', 'green', 'blue', 'red'];
  var current_color_index = colors.indexOf(paragraph.style.color);
  var new_color_index = current_color_index === colors.length - 1 ? 0 : current_color_index+1;
  paragraph.style.color = colors[new_color_index];
}
<p id='par' style='color:green'>Something</p>
<button onclick="forward()">TGo On !</button>

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