Use "tabs" in textarea field
Is it possible to use "ta开发者_StackOverflowbs"(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.
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.
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.
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!
精彩评论