Skip to content Skip to sidebar Skip to footer

Jquery: Convert Textarea Content To Html String And Vice Versa

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 e

Solution 1:

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/

Solution 2:

Well, it's not very difficult:

$('#element').html($('textarea').html().replace(/\n/g, "<br />"));

Solution 3:

Consider looking into a markdown editor. Stack overflow uses a modified version of this one: http://wmd-editor.com/

Post a Comment for "Jquery: Convert Textarea Content To Html String And Vice Versa"