Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Convert TextArea content to html string and vice versa

Tags:

what I'm trying to do is converting a TextArea content into a valid html code. Suppose you type inside the TextArea and then you press a button that shows the typed text inside a element. If you typed something like inside the TextArea:

Hi folks!
Do you like jQuery?
I do!

The resulting string you have to put inside the '' element is:

Hi folks!<br>Do you like jQuery?<br>I do!

That's because the newline inside the TextArea must be converted to the <br> tag!

The same thing should happend if you want to take the html of the element and put it inside the TextArea input field, for example:

Hi folks!<br>Do you like jQuery?<br>I do!

should be converted to:

Hi folks!
Do you like jQuery?
I do!

So, is there a way to convert a string to html-string (and vice versa) or should I write a function by myself?

Thanks in advance!

like image 802
iakko Avatar asked Sep 27 '10 13:09

iakko


1 Answers

With the appropriate styling on the element, you shouldn't need to convert anything. Using the CSS white-space property with a value set to pre, any white-space in the element should appear exactly as it does in the textarea:

#myElement { white-space: pre; }

Example: http://jsfiddle.net/TkpSu/

like image 180
Andy E Avatar answered Sep 21 '22 00:09

Andy E