Reputation: 83
I want to replace spaces in a string with a html <br />
tag, whats the best way of doing this?. Currently I have this code which replaces the spaces with a "-".
$(".msn").each(function() {
$(this).text($(this).text().replace(/ /g, '-'));
});
Upvotes: 0
Views: 5530
Reputation: 144689
$(".msn").html(function(i, oldHTML) {
return oldHTML.replace(/ /g, '<br/>');
});
Upvotes: 5
Reputation: 207901
Try using .html()
instead of .text()
:
$(".msn").each(function() {
$(this).html($(this).html().replace(/ /g, '<br />'));
});
Upvotes: 0