2

Within my HTML, I have to change a "-" to a carriage return (<br />). I have to achieve that through jQuery.

Hence, for my following HTML:-

First sentence - Second Sentence

I did this:-

caption = caption.replace("-", "<br /><br />");
$('.jcarousel-caption').text(caption);

Result:-

First Sentence &lt;br /&gt;&lt;br /&gt; Second Sentence

I even tried to change my jQuery to:-

caption = caption.replace("-", "<br /><br />");
caption.replace('&lt;','<').replace('&gt;','>').replace('&quot;','"');
$('.jcarousel-caption').text(caption);

But it still shows the same output.

I want to achieve this:

First sentence Second Sentence

1

1 Answer 1

3

The problem is you are using .text()

We need to be aware that this method escapes the string provided as necessary so that it will render correctly in HTML. To do so, it calls the DOM method .createTextNode(), does not interpret the string as HTML. Consider the following HTML:

Since you want to print the html content, you need to use .html() not .text()

$('.jcarousel-caption').html(caption);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.