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

84
Vistas
jQuery - thousands seperator on html string

I'm trying to add a thousands separator for the .htm(price * num) result by adding .digits(); to the var sub. How do I get the thousands seperator to work on the result of the var sub? Since they are not val do I need to convert the result into a number before adding the digits(); function?

$(document).ready(function() {
  $.fn.digits = function() {
    return this.each(function() {
      $(this).val(
        $(this)
        .val()
        .replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")
      );
    });
  };
  var total = $(".slider__total").html();
  var sold = $(".sold").html();
  var available = total - sold;
  var price = $(".cost").html();
  var num = $("#num").html();
  $(".item__available").html(available);
  var sub = $("#slider_subtotal")
    .html(price * num)
    .digits();
  $(".qty").attr({
    max: available
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
sold<span class="sold">3</span>total<span class="slider__total">10</span>available<span class="item__available"></span><input type="range" min="1" value="1" class="qty" name='quantity' oninput="num.value = this.value"><output id="num">0</output>unit:
$
<span class="cost">500</span>subtotal: $<span id="slider_subtotal">0</span>

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

0

You could use the built-in Number.prototype.toLocaleString() method instead:

(1234567.567).toLocaleString('en-US') // 1,234,567.567

Or - if you prefer - you can also keep it "local" to the current user by omitting the first argument: (1234567.567).toLocaleString() will refer to the browsers regional settings and will return a suitable local number string.

$(document).ready(function() {
  var total = $(".slider__total").text();
  var sold = $(".sold").text();
  var available = total - sold;
  var price = $(".cost").text();
  
  $(".item__available").html(available);
  $(".qty").attr({max: available})
           .on("input",function(){
    $("#slider_subtotal").text((price * this.value).toLocaleString("en-US",{style:'currency', currency:'USD'}));
    $("#num").text(this.value)
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
sold<span class="sold">37</span>
total<span class="slider__total">103</span>
available<span class="item__available"></span>
<input type="range" min="1" value="1" class="qty" name='quantity'>
<output id="num">0</output>
unit:$<span class="cost">123.4</span>
subtotal: <span id="slider_subtotal">$0</span>

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