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

160
Views
How do I change the background color of button after clicking on it?

I need to change the background color of the button from dark to green

<button class="btn btn-dark" id="btn" th:attr="onclick=|vote('${listAnswer.answer_id}','+1')|"><i class="fa fa-star fa-2x" ></i>UpVote</button> 


I tried to changed the button's background color from dark to green using following code. It does not work. Please help me

function vote(answerId,rateValue){

         alert(rateValue);

         axios.post('/saveRatings?answerId=' + answerId + '&rateValue=' + rateValue, {


         })
         .then(function(response){


      document.getElementById("btn").style.background-color='green';

         })

         }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

JavaScript property names cannot contain the - character because this is an operator.

Due to this, CSS properties must be accessed with camelCase syntax.

Instead of :

document.getElementById("btn").style.background-color = 'green';

Use this :

document.getElementById("btn").style.backgroundColor = 'green';
about 4 years ago · Juan Pablo Isaza Report

0

After you have clicked a button it's state becomes "active" and you can use css to change the color of the button to whatever you want. All you need to do is add a css rule using the ":active" selector. https://www.w3schools.com/cssref/sel_active.asp https://www.freecodecamp.org/news/css-button-style-hover-color-and-background/ Basically:

button:active { color: green; }  
about 4 years ago · Juan Pablo Isaza Report

0

You can try this way

<button class="btn btn-dark" id="btn" th:attr="onclick=|vote('${listAnswer.answer_id}','+1')|"><i class="fa fa-star fa-2x" ></i>UpVote</button>

JS

const btn = document.getElementById('btn');

btn.addEventListener('click', function onClick() {
  btn.style.backgroundColor = 'green';
  btn.style.color = '(Color whatever you want)';
});

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!