Reputation:
I want to display the text on the page with linebreak,
<html>
<body>
<div class="content">one,two,three</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(".content").text(function(i, val){
return val.replace(/,/g, "<br>");
});
</script>
</body>
</html>
but what i get is:
one<br>two<br>three
Upvotes: 1
Views: 69
Reputation: 167172
The <br />
is a HTML tag. Use .html()
instead of .text()
:
$(".content").html(function(i, val){
return val.replace(/,/g, "<br />");
});
Working Snippet
$(function () {
$(".content").html(function(i, val){
return val.replace(/,/g, "<br />");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content">one,two,three</div>
Upvotes: 2