Brad Guy Brad Guy - 21 days ago 11
jQuery Question

Formatting slider values with commas

$("#slider-range").slider({
range: true,
min: 0,
max: 200000,
step: 500,
values: [2500, 25000],
slide: function(event, ui) {
$("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]);
}
});
$("#amount").val("$" + $("#slider-range").slider("values", 0) + " - $" + $("#slider-range").slider("values", 1));


How do I format the outcome: $2,500 - $20,000

Answer

You can wrap $("#slider-range").slider("values", 0) as a parameter and pass it to function which would take that value and with help of regular expression replace() method return a new string with matched pattern:

function ThousandSeparate(val) {
    while (/(\d+)(\d{3})/.test(val.toString())){
      val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
    }
    return val;
}

Check working code: jsFiddle