Aakash Chakravarthy
Aakash Chakravarthy

Reputation: 10701

Use "tabs" in textarea field

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.

Upvotes: 3

Views: 2496

Answers (3)

Nick Craver
Nick Craver

Reputation: 630637

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.

Upvotes: 5

Luca Matteis
Luca Matteis

Reputation: 29267

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.

Upvotes: 1

Vijay Dev
Vijay Dev

Reputation: 27496

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!

Upvotes: 0

Related Questions