Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda