Solved

Getting the Tab Key to Indent in Textarea box

Posted on 2003-12-02
13
628 Views
Last Modified: 2010-04-09
I've got a textarea box I'm using for an html powered CMS and I'm wanting to tab my code in all the time using the tab key, but using it moves the cursor from the text box.  I can't easily tab.

Any easy ways around this?  JS?
0
Comment
Question by:happydog234
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 31

Accepted Solution

by:
seanpowell earned 500 total points
ID: 9863723
This works for Ctrl-Tab, IE only...

copyright removed as per members agreement...

function CheckTab(el) {
  // Run only in IE
  // and if tab key is pressed
  // and if the control key is pressed
  if ((document.all) && (9==event.keyCode) && (event.ctrlKey)) {
    // Cache the selection
    el.selection=document.selection.createRange();
    setTimeout("ProcessTab('" + el.id + "')",0)
  }
}

function ProcessTab(id) {
  // Insert tab character in place of cached selection
  document.all[id].selection.text=String.fromCharCode(9)
  // Set the focus
  document.all[id].focus()
}
</SCRIPT>
<TEXTAREA ID=MyTabDemo ONKEYDOWN="CheckTab(this)" ROWS=20 COLS=20>
Try using CTRL-TAB inside of this textbox.
</TEXTAREA>
0
 

Expert Comment

by:elvin226
ID: 9865430
I went to this section to ask exactly the same question. And it's also the latest entry in the unanswered questions list! Thanks to happydog and georgemarian. :)
0
 

Expert Comment

by:Nelson_Pires
ID: 9866361
Alternatively and to make it multi browser compatible, add a Text Link, say TAB, or a Image and when you click on it it will add the tab, here's the JS function for that:

function addTAB() {
      document.frmNAME.txtNAME.value += "    ";
      document.frmNAME.txtNAME.focus();
}

There's a little draw back to this, it will aways add the Tab to the end, I can give the more complete code where it adds the Tab where the cursor is, but it only works in IE.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:happydog234
ID: 9867706
Well seanpowell, it works fine.  So thanks.

But I'm going to hold off awardiung you points to see if I someone can figure out not having to use any key with tab, just tab.

0
 

Expert Comment

by:Nelson_Pires
ID: 9869549
That would not be possible, because, Tab key by default changes the focus amongst all controls in the page, that's the default behaviour and can't be changed.
0
 
LVL 31

Expert Comment

by:seanpowell
ID: 9869596
Hi happydog234,

We need to modify the initial keystroke to maintain user-accessibility. The only way to do it would potentially break the standard operating environment - so you'd correct one thing by corrupting another.

Given the general usage patterns of multiple keystrokes to perform an action (Ctrl-C) for instance, I don't believe adding the extra element would create any interface problems for the user....

Thanks,
georgemarian
0
 

Author Comment

by:happydog234
ID: 9870483
IE 5.5+ specific is fine.
0
 
LVL 31

Expert Comment

by:seanpowell
ID: 9870683
Okay - so we'll leave it as is. It's the best option...
0
 

Author Comment

by:happydog234
ID: 9871451
seanpowell ,

I would agree you shouldn't break standard practice.  This CMS is only for me, on my own site, so I don't mind breaking it.

The above code with CTRL, can it be changed to just tab?
0
 

Author Comment

by:happydog234
ID: 9871456
It does work seanpowell !

Nice!
0
 
LVL 31

Expert Comment

by:seanpowell
ID: 9871504
>>The above code with CTRL, can it be changed to just tab?
Absolutely - we just remove the additional CTRL keypress statement:

<html>
<head>
<script>
function CheckTab(el) {
  // Run only in IE
  // and if tab key is pressed

  if ((document.all) && (9==event.keyCode)) {
    // Cache the selection
    el.selection=document.selection.createRange();
    setTimeout("ProcessTab('" + el.id + "')",0)
  }
}

function ProcessTab(id) {
  // Insert tab character in place of cached selection
  document.all[id].selection.text=String.fromCharCode(9)
  // Set the focus
  document.all[id].focus()
}
</SCRIPT>
</head>
<body>
<form>
<TEXTAREA ID=MyTabDemo ONKEYDOWN="CheckTab(this)" ROWS=20 COLS=20>
Try using just the TAB inside of this textbox.
</TEXTAREA>
</form>
</body>
</html>
0
 

Expert Comment

by:Nelson_Pires
ID: 9874036
You are using CreateRange object, beware only IE can deal with it, NS will just ignore it at best.

Good luck.
0
 
LVL 31

Expert Comment

by:seanpowell
ID: 9874362
They mentioned already that IE 5.5+ specific is fine...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
links not working 2 34
aria difference 2 22
title attribute 5 33
Trying to use &NBSP to line up 3 rows 12 38
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question