Reputation: 717
trying to take this content:
<div class="content">one,two,three</div>
<div class="content">four,five,six</div>
<div class="content">seven,eight,nine</div>
and .split and .join using jQuery's each.
$('.content').each(function() {
var mydata = $(this).text().split(',').join(", ");
$(this).text(mydata);
});
fiddle: http://jsfiddle.net/ZXgx2
Upvotes: 4
Views: 25911
Reputation: 60493
you solution is fine, your fiddle is wrong : split(', ')
Upvotes: 2
Reputation: 268344
No reason to split, join, or call .each
. Just modify the text of all .content
elements via a quick regex:
$(".content").text(function(i,v){
return v.replace(/,/g, ", ");
});
Fiddle: http://jsfiddle.net/jonathansampson/uAHBU/
Upvotes: 1
Reputation: 145398
Of course you can use split
and join
:
$(".content").text(function(i, val) {
return val.split(",").join(", ");
});
But I'd recommend to use regular expression instead:
$(".content").text(function(i, val) {
return val.replace(/,/g, ", ");
});
DEMO: http://jsfiddle.net/ZXgx2/6/
Upvotes: 14