Home > Java, Web > Allowing tabs in a text area

Allowing tabs in a text area

by paul on March 10, 2008

In addition to controlling the maximum number of characters in a text area, I also want to allow the user to enter tabs. But default, hitting tab would move out of the text area to the next control on the page.

Overriding this behavior can be done by using the onKeyDown event controller so you can:

  1. check if the tab key has been pressed
  2. if it has, insert a tab at the current cursor location
  3. put focus back into the text area

I’ve extended my little TextAreaTagLib to handle an ‘allowTabs’ attribute. Now you can allow tabs to be entered just by using allowTabs=”true”. I’ve also incorporated the work from the TextField modification so that the number of characters can be limited based on the maxSize constraint on the object.

Sample use:

<g:javascript library=”prototype”/>
<jttext:script />

<jttext:textArea object=”${new Book()}” name=”content” allowTabs=”true”/>
<jttext:statusDiv for=”content” />

Please note: I’ve now called the tag lib JTFormTagLib.groovy, and it has both the TextArea and the TextField code in it.

Download here.

Also, I’ve thought about creating it as a plugin, but it’s kind of small to warrant a plugin by itself – what do you think?

{ 4 comments… read them below or add one }

Dierk König March 10, 2008 at 8:29 pm

No useful functionality is too small for a plugin ;-)
Constructing a new plugin really is a piece of cake and users have much less hassle with
- finding the functionality in the first place
- installing it
- keeping it up to date (!)
Your latest enhancement with allowsTabs is the best example.

cheers
Dierk

paul March 11, 2008 at 3:24 pm

Okay, I’ve created a plugin, I’ll make it available soon. (I’ve got to get around to putting all of my plugins in the source repo too).

paul March 12, 2008 at 12:57 am
ted devito January 11, 2009 at 8:26 pm

hey there.

i found your site while i was looking for the right solution so i figured i’d come back and share what i came up with in case it is helpful.

http://teddevito.com/demos/textarea.html

works in Chrome 1.0, FF 3.0, IE 7, Safari 3.2 (opera still has some focus() issues)

lets you TAB or SHIFT+TAB, single or multiple lines at one time.

enjoy!

Leave a Comment

Previous post:

Next post: