Sam San
Sam San

Reputation: 6903

PHP print content as it is

I have a textarea

<textarea id=view-body ></textarea>

that gets a content when an onclick from

<a href='#' onclick='popup_view(this)' data-body ='". $rows['body'] ."' >view</a>

<script language="JavaScript">
function popup_view(e) 
{       
    var body = e.getAttribute('data-body');         
    el = document.getElementById("view-body");  
    el.innerHTML = body;    
}
</script>

I wanted the content of my textarea to be printed the way it is. For example the onclick sends

<i>Wired</i>

my textarea printout

<i>Wired</i>

but not, and supposed to be like this

Wired

Upvotes: 1

Views: 154

Answers (5)

Anas
Anas

Reputation: 366

You must use the htmlentities function to replace special characters with html entities.

data-body = "<? php echo htmlentities($rows[' body ']);?>"

Upvotes: 0

VibhaJ
VibhaJ

Reputation: 2256

Javascript innerHTML function will convert text to html form.

You can use textContent function as shown here

http://jsfiddle.net/yWgn7/

In jquery you can use .text() function, but i am not sure about textContent compatibility.

Upvotes: 2

Sumesh TG
Sumesh TG

Reputation: 450

You can replace your text-area with any Rich text editor http://ckeditor.com/

Upvotes: 0

complex857
complex857

Reputation: 20753

HTML's built in <textarea> elements doesn't allow you to style it's contents. What you need is some kind of a wysiwyg editor to do that.

Upvotes: 0

CLo
CLo

Reputation: 3730

Instead of a textarea try using a div. If I'm understanding this correctly, the textarea is showing the <i></i> and you don't want it to. Textarea doesn't handle html formatting naturally.

Upvotes: 1

Related Questions