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

214
Views
How can I change the background color of a variable that ends in .value in javascript
function click() {
  let principal = document.getElementById("principal").value;
  let rate = document.getElementById("rate").value;
  let years = document.getElementById("years").value;
  let interest = principal * years * rate / 100;
  let year = new Date().getFullYear() + parseInt(years);

  document.getElementById("result").innerHTML = "If you deposit " + principal + ",\<br\>at an interest rate of " + rate + "%\<br\>You will receive an amount of " + interest + ",\<br\>in the year " + year + "\<br\>"
}

Instead of the rate, principal, rate, interest, year, I need their values with a yellow background color, however, rate.style.background = "yellow" is not possible. How could I change the variables background color in the result?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You need to change the DOM style, and not it's value like you did there, you can query the DOMs (so you can manipulate the styles) and ask for value every time, like this:

function click() {
  let principal = document.getElementById("principal");
  let rate = document.getElementById("rate");
  let years = document.getElementById("years");
  let interest = principal.value * years.value * rate.value / 100;
  let year = new Date().getFullYear() + parseInt(years);

  document.getElementById("result").innerHTML = "If you deposit " + principal.value + ",\<br\>at an interest rate of " + rate.value + "%\<br\>You will receive an amount of " + interest.value + ",\<br\>in the year " + year.value + "\<br\>"
}
about 4 years ago · Juan Pablo Isaza Report

0

As mentioned in the comment, there are a few things that should be fixed:

  1. document.getElementById().value returns the value attribute of a DOM element and the attribute has no background property that can be modified. In order to do what you want to achieve, you have to wrap that value in a DOM element (e.g. a <span>) and give the background to that element
  2. document.getElementById().value returns the value as a string; if you want to do math with it, you have to convert the value to a number

When you fix that, you should end up with something similar to:

function clickFn() {
  let principal = parseFloat(document.getElementById("principal").value);
  let rate = parseFloat(document.getElementById("rate").value);
  let years = parseInt(document.getElementById("years").value);
  let interest = principal * years * rate / 100;
  let year = new Date().getFullYear() + years;

  document.getElementById("result").innerHTML = "If you deposit <span class='yellow'>" + principal + "</span>,<br\>at an interest rate of <span class='yellow'>" + rate + "%</span><br\>You will receive an amount of <span class='yellow'>" + interest + "</span>,<br\>in the year <span class='yellow'>" + year + "</span><br\>";
  
  /*
  You could use a templating string instead of string concatenation, to make your code a little bit more compact:
  
  document.getElementById("result").innerHTML = `If you deposit <span class='yellow'>${principal}</span>,<br\>at an interest rate of <span class='yellow'>${rate}%</span><br\>You will receive an amount of <span class='yellow'>${interest}</span>,<br\>in the year <span class='yellow'>${year}</span><br\>`
  */
}

document.getElementById("calculate").addEventListener('click', clickFn);
.yellow {
  background-color: yellow;
}
<input id="principal" name="principal" type="number" />
<input id="rate" name="rate" type="number" />
<input id="years" name="years" type="number" />

<button id="calculate">calculate</button>

<p id="result">

</p>

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!