views:

58

answers:

3

Hello,

Is it possible to use "tabs"(indenting) in textarea using javascript. When tab button is clicked, the next form element is focused. But, i need to indent the text in textarea.

I am currently working in a project and any code with javascript or jquery will help me.

A: 

Try this: capture the tab key in keypress events and stop propagation. I m not sure if this will work consistently across browsers; give it a try!

Vijay Dev
A: 

What about something like this:

<input onkeypress="if(event.keyCode == 9) { this.value += '    '; return false; }">

Basically, when you press the keyCode 9 (which is the Tab key) I return false. This prevents from focusing another element on the page.

To mimic the Tab spaces I just add the spaces myself to the value of the input itself.

Luca Matteis
That's only going to work sensibly if the caret is at the end of the textarea.
Tim Down
A: 

The best plugin I've seen for this is the Tabs in Textarea plugin. You can try a demo on its page.

The setup is fairly simple, since it has a simple effect:

$("textarea").tabby();

The thing that annoyed me most about other plugins was lack of shift+tab, which this handles. You can do this without a plugin, but I wouldn't in this case...it's be quite a bit of code yourself, depending on the functionality you're after. TextRange operations in a cross-browser way are still a bit hairy under the covers, this is one of the times that a plugin is a better approach, IMO.

Nick Craver