Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a fake textarea

For a project I'm working on, I need to be able to show "divs" before the text contents of a textarea :

enter image description here

At first I thought I could just place the divs with absolute positioning and z-index, but that would entail "pushing" the text, and making sure that the user can't remove those first spaces, not with backspace nor ctrl+c nor ctrl+x nor delete ... It looked complicated to get every possible way.

Now, I'm trying to use a "div" made to look like a textarea, which contains an editable "span" that will contain the text :

enter image description here

That works for the moment, but it's not perfect, especially in terms of :focus (clicking anywhere on the outer div should display the cursor in the text span*), and it seems to break if I empty the text span.

Any ideas on how to fix this ? I'm open to suggestions, even if I have to change the structure of my fake textarea.

It should work on all major (recent) browsers, and can use jQuery.

  • $('#outerDiv').bind('click', $('#outerDiv span.text').focus()); seems to work in Chrome but not in firefox.
like image 574
Manu Avatar asked Feb 21 '12 09:02

Manu


People also ask

How do you create a textarea?

The <textarea> element is often used in a form, to collect user inputs like comments or reviews. A text area can hold an unlimited number of characters, and the text renders in a fixed-width font (usually Courier). The size of a text area is specified by the <cols> and <rows> attributes (or with CSS).

How make textarea non editable?

The readonly attribute is a boolean attribute. When present, it specifies that a text area should be read-only. In a read-only text area, the content cannot be changed, but a user can tab to it, highlight it and copy content from it.

Can textarea have ID?

The id attribute assigns an identifier to the <textarea> element. The id allows JavaScript to easily access the <textarea> element. It is also used to point to a specific id selector in a style sheet. Tip: id is a global attribute that can be applied to any HTML element.


1 Answers

What I'd do is the following:

  1. Your containing element (the text editor) should be a block level element. It is not editable.
  2. Your tags should consist of the following: A container that floats to the left and inline-block or floated children.
  3. A non-floated block (important) level element that is contenteditable.

End result:

<div>
  <!-- The list of tags -->
  <ul style="float:left"> 
    <li style="float:left">...</li>
    <li style="float:left">...</li>
    <li style="float:left">...</li>
  </ul>
  <!-- This will contain your text: -->
  <div contenteditable="true">...</div>
</div>

If you have a lot of tags, they'll wrap to the next line. Text in the editable element will wrap around the tags as well.

Clicking the tags would not give the editable element focus, but you can remedy that with JavaScript.

Here's an example I whipped up. Works in Safari/Chrome/Firefox. Have not tested Internet Explorer, but it should work fine.

like image 60
RickN Avatar answered Sep 21 '22 15:09

RickN