For your understanding, your code is currently working like this one :
var $anotherstring = 'hello,world';
$(".content").text(function(i, $string) {
return $string.replace(/,/g, ", ");
});
(because the inner variable parameter of function is not the same as the first one, and hide the first $string
you defined with the same name)
The way you are using text
with the inner function(i, $string)
will work if the current text value of the element is 'hello,world'
(see the jquery documentation)
$(".content").text('hello,world'); // just to set the value of the .content element for the example, you can ignore this if this already the case
$(".content").text(function(i, $string) {
return $string.replace(/,/g, ", ");
});
So the above code will work if you want to transform the existing content.
If you simply want to use the value of another variable, and not the old content of the .content element, you can simply do :
var $string = 'hello,world';
$(".content").text($string.replace(/,/g, ", "))