user5544269
user5544269

Reputation: 21

Replace a Text in an Element along with maintaining a new line

I'm trying to change a text in the div tag "a/b" to "b/c" which would also maintain the new line that I had set up in the HTML using the break tag. Right now it replaces the text, but I want the second line to be place under the first line.

$(".text_div").text(function () {
    return "<br />" + $(this).text().replace("a/b", "b/c"); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="text_div">
    This div contains some text.<br />
    This is a a/b.
</div>

Upvotes: 0

Views: 21

Answers (1)

chiliNUT
chiliNUT

Reputation: 19573

use .html() instead of text if you want to keep the line breaks

$(".text_div").html(function () {
    return $(this).html().replace("a/b", "b/c"); 
});

Upvotes: 1

Related Questions