textarea

Creating a textarea with auto-resize

£可爱£侵袭症+ 提交于 2019-11-25 22:22:37
问题 There was another thread about this, which I\'ve tried. But there is one problem: the textarea doesn\'t shrink if you delete the content. I can\'t find any way to shrink it to the correct size - the clientHeight value comes back as the full size of the textarea , not its contents. The code from that page is below: function FitToContent(id, maxHeight) { var text = id && id.style ? id : document.getElementById(id); if ( !text ) return; var adjustedHeight = text.clientHeight; if ( !maxHeight ||

How to get the caret column (not pixels) position in a textarea, in characters, from the start?

…衆ロ難τιáo~ 提交于 2019-11-25 22:17:29
问题 How do you get the caret position in a <textarea> using JavaScript? For example: This is| a text This should return 7 . How would you get it to return the strings surrounding the cursor / selection? E.g.: \'This is\', \'\', \' a text\' . If the word “is” is highlighted, then it would return \'This \', \'is\', \' a text\' . 回答1: With Firefox, Safari (and other Gecko based browsers) you can easily use textarea.selectionStart, but for IE that doesn't work, so you will have to do something like

Rendering HTML inside textarea

天大地大妈咪最大 提交于 2019-11-25 22:02:26
问题 I need to be able to render some HTML tags inside a textarea (namely <strong>, <i>, <u>, <a>) but textareas only interpret their content as text. Is there an easy way of doing it without relying on external libraries/plugins (I\'m using jQuery)? If not, do you know of any jQuery plugin I could use to do this? 回答1: This is not possible to do with a textarea . What you are looking for is an content editable div, which is very easily done: <div contenteditable="true"></div> jsFiddle div.editable

Cursor position in a textarea (character index, not x/y coordinates)

时间秒杀一切 提交于 2019-11-25 21:50:05
How can I get the caret's position in a textarea using jQuery? I'm looking for the cursor's offset from the start of the text, not for the (x, y) position . Not jQuery, but just Javascript... var position = window.getSelection().getRangeAt(0).startOffset; Ryan Modified BojanG's solution to work with jQuery. Tested in Chrome, FF, and IE. (function ($, undefined) { $.fn.getCursorPosition = function() { var el = $(this).get(0); var pos = 0; if('selectionStart' in el) { pos = el.selectionStart; } else if('selection' in document) { el.focus(); var Sel = document.selection.createRange(); var

JavaScript: How to add line breaks to an HTML textarea?

谁说胖子不能爱 提交于 2019-11-25 20:39:30
I'm editing a textarea with JavaScript. The problem is that when I make line breaks in it, they won't display. How can I do this? I'm getting the value to write a function, but it won't give line breaks. TStamper Problem comes from the fact that line breaks ( \n\r ?) are not the same as HTML <br/> tags var text = document.forms[0].txt.value; text = text.replace(/\r?\n/g, '<br />'); jit if you use general java script and you need to assign string to text area value then document.getElementById("textareaid").value='texthere\\\ntexttext'. you need to replace \n or < br > to \\\n otherwise it