2

I am using a comment box and insert the value to mysql using ajax. Same time I append the entered comment with jQuery to the same page. The problem is if the entered text is some JavaScript code, like

<script>alert('hello');</script>

it shows the alert. In my database I sanitize these code but since I append it in client side I need to display the entered text like it is.

Well I use,

<div contenteditable="true"></div>

Any immediate help, please?

2 Answers 2

1

The following snippet HTML encodes string to:

"&lt;div&gt;Here is an HTML&lt;/div"


$("p").text("<div>Here is an HTML</div").html()
Sign up to request clarification or add additional context in comments.

Comments

0

I used this method!

str.replace(/</g, "&lt;");
str.replace(/>/g, "&gt;");

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.