user9782404
user9782404

Reputation:

How to implode + add linebreak?

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

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

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

Related Questions