Format text in a <textarea>?

陌路散爱 提交于 2019-11-26 12:27:08

If you need to customize your textarea, reproduce its behavior using another element (like a DIV) with the contenteditable attribute.

It's more customizable, and a more modern approach, textarea is just for plain text content, not for rich content.

<div id='fake_textarea' contenteditable></div>

The scrollbars can be reproduced with the CSS overflow property.

You can use this fake textarea in a form normally, i.e: if you have to submit its content through POST method, you could do something like(with jQuery):

<input type='hidden' id='fake_textarea_content' name='foobar' />

...

$('#your_form').submit(function()
{
  $('#fake_textarea_content').val($('#fake_textarea').html());
});
Deep

You Can't style the content of a text area separately, you have to use <div>s, or something similar.

Do you Want Something like this:?

http://jsfiddle.net/mekwall/XNkDx/

$('.editable').each(function(){
    this.contentEditable = true;
});

This allows you to edit the content of a div, and it will still look like a textarea,

Bold will now Work.

You cannot use HTML inside TEXTAREA.

Scrolling can be applied to any element by adding overflow: auto and fixed width and/or height.

You can user html editors for web like CKEditor to be able to format the data in text area. Check this http://ckeditor.com/

Another way to submit the "fake" text area is including the following lines inside the form tag

<form onsubmit=" $('#fake_textarea_content').val($('#fake_textarea').html());">
</form>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!