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.
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.
Comments