Devyn
Devyn

Reputation: 427

Remove Commas with Jquery

I am in need of some code that removes commas from a string. I currently have a variety of numbers in the number_format() for PHP. I use Jquery to post certain things to a update page and I need the commas removed from a class.

for instance here is some code.

<span class="money">1,234,567</span>

I want the value of the code I post to be 1234567 instead of 1,234,567. I would like to use Jquery if that's possible.

Thanks

Upvotes: 20

Views: 48198

Answers (6)

Peyu_Atrellu
Peyu_Atrellu

Reputation: 61

This is a clean way to get rid of the commas in one line, without loops.

    var cleanNumber = $("#selector").val().split(",").join("");

Hope can help u!

Upvotes: 6

g.m.ashaduzzaman
g.m.ashaduzzaman

Reputation: 2369

Could you please try with this , will remove comma on click on textbox , after focusout thousand seperated will be added...

$( "#salary" ).click(function() {

   $( "#salary" ).val(  $("#salary").val().replace(/,/g, ''));

});



$( "#salary" ).blur(function() {

   $( "#salary" ).val( addCommas($( "#salary" ).val());

});

function addCommas(nStr) {

    nStr += '';
    var x = nStr.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;

}

Upvotes: 0

Gaurav Agrawal
Gaurav Agrawal

Reputation: 4431

use replace function of javascript for removing , in a string

var result = jQuery('.money').html().replace(',', '');
alert(result);

Upvotes: -1

Kevin Redman
Kevin Redman

Reputation: 459

Less is more! (sometimes!)

$(document).ready(function(){
var **stripString** = $('.money').text().replace(/,/g, '');
$('.*money*').text(**stripString**);
});

Upvotes: 4

Jayantha Lal Sirisena
Jayantha Lal Sirisena

Reputation: 21366

you can do like this

var str=$(".money").text();
var str2=   str.replace(",", "")

Upvotes: 2

Joe
Joe

Reputation: 82614

replace

var noCommas = $('.money').text().replace(/,/g, ''),
    asANumber = +noCommas;

Upvotes: 44

Related Questions