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

63
Vistas
Highlighting Part of a Result

I'm trying to highlight ONLY the variables: principal, rate, interest, year in the return statement of the compute function. It is required of me that all styling must be in the CSS file only. No inline or internal stylesheets are allowed. All code must be in the JavaScript file only. I have created the highlight class in CSS but this highlights the whole sentence and I don't know how to only highlight parts of a return statement without using inline.

Could someone please help/guide me to know how it's done cause I'm new to using these languages? Would be much appreciated.

Kindly find the HTML, CSS, and JavaScript code below.

HTML CODE

<!--- On clicking the button it computes the interest by calling the compute() function --->
<button onclick="compute()">Compute Interest</button><br><br>
<!--- Show computation result --->
<span id="result" class = "highlight"></span>

CSS CODE

/* Highlights the result */
.highlight {
    background-color: yellow;
}

JAVASCRIPT CODE

/* Function that computes and returns the result */
function compute()
{
    /* some code */
   
 return 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\>";
}

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

For this to work you would need span-elements with the class highlight around you're variables. For semantics it might be better if the element with the id result is a p-element or something similar.

Also as @user1599011 pointed out, your Javascript needs to do something, it needs to execute a command, not return it.

A working solution:

let principal = '1000';
let rate = '10';
let interest = '100';
let year = '2022';

/* Function that computes and returns the result */
function compute()
{
  document.getElementById("result").innerHTML= 'If you deposit <span class="highlight">' +principal+ '</span>, <br>at an interest rate of <span class="highlight">' +rate+ '%</span><br>You will receive an amount of <span class="highlight">' +interest+ '</span>, <br>in the year <span class="highlight">' +year+ '</span><br>';
}
/* Highlights the result */
.highlight {
    background-color: yellow;
}
<!--- On clicking the button it computes the interest by calling the compute() function --->
<button onclick="compute()">Compute Interest</button>
<!--- Show computation result --->
<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