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

213
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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