Replace comma with period jQuery

Matthew

I need to replace the commas in each <li> value with a period.

I have no idea what's wrong with my code. I checked the console, but nothing...

$('#stats li').each(function () {
    $(this).text().replace(/,/g, '.');
});

This code should target each <li> in <ul id="stats">. Then it should replace each , in each <li> and replace it with a .

I also tried this:

$('#stats li').each(function () {
        var comma = /,/g;
        if(comma.test($this)) {
            $(this).replace(comma, '.');
        }
});

And I tried this:

$('#stats li').each(function () {
    var stats = [];
    stats.push($(this).text());
    stats.replace(/,/g, '.');
    console.log(stats);
});

Here is the Fiddle.

p.s.w.g

The problem is that the replace method returns a new string. It does not modify the string in place. Try this instead:

$('#stats li').each(function () {
     $(this).text($(this).text().replace(/,/g, '.'));
});

But for that matter, jQuery's text method accepts a function, too. This is a lot cleaner:

$('#stats li').text(function (index, text) { 
    return text.replace(/,/g, '.');
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Replace last comma in character with " &"

From Dev

Need to replace "comma" with 'dot'

From Dev

Solr to Tokenize on white space, comma and period

From Dev

Replace comma with OR conditional (PHP)

From Dev

Replace dot with comma on SELECT

From Dev

Validating Positive number with comma and period

From Dev

Replace comma with newline in java

From Dev

Replace a dot with a comma inside a jQuery template

From Dev

Replace comma separated values

From Dev

Replace final comma in a string with "and"

From Dev

Replace ,(comma) by .(dot) and .(dot) by ,(comma)

From Dev

Replace a period with HTML character

From Dev

how to replace comma with dot

From Dev

Replace comma with a dot while typing jQuery

From Dev

Regex to pick up comma and period Python

From Dev

Replace a dot with a comma inside a jQuery template

From Dev

Comma instead of period

From Dev

Replace everything that is not a letter,single quote,comma,period,question mark, or exclamation mark

From Dev

Using .replace to check regex for space, comma and period?

From Dev

Period has become a comma after update

From Dev

regex for pattern between comma and period

From Dev

Replace comma with a comma followed by space

From Dev

Replace dot character once entered by a comma in jquery

From Dev

GAS replace comma with tab

From Dev

Best way to replace comma with a dot jquery

From Dev

C#: Writing files with comma or period slow

From Dev

regex for substituting comma with period in a string of digits

From Dev

Replace comma and newline with sed

From Dev

How to change decimal comma to decimal period in numpad?