christianleroy
christianleroy

Reputation: 1104

HTML inside TextArea?

So I have this textarea in my website. By default, it has something like this as its contents:

Name : Sample Value
Age : Sample Value
Location : Sample Value

It is editable before the user hits the button and inserts it into the database, although I am not using a rich text editor since it's nothing but a simple text.

Since basic HTML codes are not browser readable inside the textarea tag, I used 
 to separate lines.

Now my problem is that I am not able to include the HTML code when I'm reading the value of the textarea tag in the server side.

Thus, the value inserted to the database is not HTML formatted as well, and when it is once again fetched into a web browser, it has no format at all.

What alternatives do I have? Thanks.

Upvotes: 2

Views: 10857

Answers (3)

Abhishek Tewari
Abhishek Tewari

Reputation: 425

The answer is very simple.
Use contenteditable DIVs instead of TextBox and TextArea.
But remember to add contenteditable="false" to all your inner HTML tags.

This worked for me.

Upvotes: 0

Gurpreet Singh
Gurpreet Singh

Reputation: 21233

Not possible using textarea, use contenteditable DIV instead.

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

You can use getters and setter as shown below:

//Get content    
var contents = document.getElementById("divId").innerHTML; 

//Set content
document.getElementById("divId").innerHTML = contents

Here is the browser support for this approach.

Upvotes: 6

Francis Tchatchoua
Francis Tchatchoua

Reputation: 65

Why don't you use JQuery and do this $(textarea).val() to get the value of the textarea as a string and use it server side. you might have to consider using Ajax to make a call to the server side method you want to pass the Html data.

Upvotes: 0

Related Questions