Link to home
Start Free TrialLog in
Avatar of happydog234
happydog234

asked on

Getting the Tab Key to Indent in Textarea box

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?
ASKER CERTIFIED SOLUTION
Avatar of seanpowell
seanpowell
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of elvin226
elvin226

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. :)
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.
Avatar of happydog234

ASKER

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.

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.
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
IE 5.5+ specific is fine.
Okay - so we'll leave it as is. It's the best option...
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?
It does work seanpowell !

Nice!
>>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>
You are using CreateRange object, beware only IE can deal with it, NS will just ignore it at best.

Good luck.
They mentioned already that IE 5.5+ specific is fine...