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

288
Vistas
Replace value from JS if found inside div based on if/else condition

I have an html code from an online product recommendation platform which allows me to add JS to html.

The HTML code below works fine, but i want to apply JS to it based on the logic that, if ${discountvalue} shows any value, then both ${discountvalue} and ${regularvalue} values are visible with cross line on regularvalue.

If ${regularvalue} does not have any value or is empty/null, then only ${regularvalue} will be visible.

HTML CODE:

<div class="slider-wrapper">
${#Recommendations} // This loads the the more then one values of product recommendation

    ${discountvalue}
    ${regularvalue}

    <div class="slider-wrapper">
      <div class="pricebox">
        <p class="dynamic-price">${discountvalue}</p>
        <p class="regular-price">
          <span class="rec_price_num">${regularvalue}</span>
        </p>
      </div>
    </div>

${/#Recommendations}
</div>

And this is the JS code:

var itemPrices = document.querySelectorAll('.slider-wrapper .pricebox');
for(var i=0; i<itemPrices.length; i++){

   var discount_price = itemPrices[i].getElementsByClassName("dynamic-price")[0].innerHTML;
   var price = getElementsByClassName("rec_price_num")[0].innerHTML;

   if(discount_price){
      var discount_price = itemPrices[i].getElementsByClassName("dynamic-price")[0].innerHTML;
      var price = getElementsByClassName("rec_price_num")[0].innerHTML;
   }
   else {
       var price = getElementsByClassName("rec_price_num")[0].innerHTML;
   }

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

0

parseFloat(discount_price.innerHTML) will return a truthy value if the discount price can be parsed to a floating point number different from 0.

You can use the CSS rule text-decoration: line-through; to have a cross line on the value.

var itemPrices = document.querySelectorAll('.slider-wrapper .pricebox');
for(var i=0; i<itemPrices.length; i++){

   var discount_price = itemPrices[i].getElementsByClassName("dynamic-price")[0];
   var price = itemPrices[i].getElementsByClassName("regular-price")[0];
   
   if(parseFloat(discount_price.innerHTML)){
     price.style.textDecoration = "line-through";
   } else {
     discount_price.style.display = "none";
   }
}
<div class="slider-wrapper">
  <div class="pricebox">
    <p class="dynamic-price">0</p>
    <p class="regular-price">5</p>
  </div>
  <hr>
  <div class="pricebox">
    <p class="dynamic-price">4</p>
    <p class="regular-price">5</p>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Don't need javascript. can be solved by css3

.pricebox p {
        font-weight: bold;
        font-size: 1.5rem;
        color: red;
        margin: 0 0 1rem;
    }
    .pricebox .dynamic-price {
        margin-bottom: 0;
    }
    .pricebox .dynamic-price:empty {
        display: none
    }
    .pricebox .dynamic-price:not(:empty) + .regular-price {
        font-size: .875rem;
        color: gray;
        text-decoration: line-through;
    }
<div class="slider-wrapper">
    <div class="slider-wrapper">
      <div class="pricebox">
        <p class="dynamic-price"></p>
        <p class="regular-price">
          <span class="rec_price_num">200.00</span>
        </p>
      </div>
      <div class="pricebox">
        <p class="dynamic-price">100.00</p>
        <p class="regular-price">
          <span class="rec_price_num">150.00</span>
        </p>
      </div>
    </div>
</div>

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