0

The scenario is the user will enter the text in HTML format (e.g. <\/b>Testing<\/b>) then the inserted text will get saved into the database(HTML code as a string (e.g. <\b>Testing<\/b>).

I want the string fetched back from the database to be displayed as HTML text (e.g. Testing).

I followed the below snippet but didn't get anything as output.

Note: <%= cData.description %> worked fine when executed simply but displayed HTML code as plain text.

test.js (route file):

var testid = 234123;
b.find(testid, function(data) {
  b.otherdet(testid, function(cdata){
    res.render('blogdesc',{
      Data: data,
      cdata: cdata
    });     
  });
});

test.ejs file:

<p class="" id="descid"></p>

<script>
  var $log = $( "#descid" );
  html = $.parseHTML('<%= cData.description %>'); //description is column in database
  $log.append( html );
</script>
2
  • 1
    Does this answer your question? Print raw html strings on EJS Commented Mar 10, 2020 at 17:18
  • Thanks @Craicerjack, It worked. Commented Mar 10, 2020 at 17:24

2 Answers 2

1

https://stackoverflow.com/a/8125053/20394 shows how to emit HTML as-is in EJS, but please make sure that you sanitize that content to avoid XSS.

Sign up to request clarification or add additional context in comments.

Comments

1

I've found, where did it go wrong. I was using:

html = $.parseHTML('<%=blogData.description%>');

while the actual syntax should be this:

html = $.parseHTML('<%-blogData.description%>');

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.