user1840724
user1840724

Reputation: 83

Jquery Replacing space in string with html <br /> tag

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

Answers (2)

Ram
Ram

Reputation: 144689

$(".msn").html(function(i, oldHTML) {
    return oldHTML.replace(/ /g, '<br/>');
});

Upvotes: 5

j08691
j08691

Reputation: 207901

Try using .html() instead of .text():

$(".msn").each(function() {
    $(this).html($(this).html().replace(/ /g, '<br />'));
});

Upvotes: 0

Related Questions