Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!