Tabbing in a textarea?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Tabbing in a textarea?

Post by JellyFish »

How do you make it to where when you press the "Tab" key in a textarea, it doesn't go to the next element in the page but rather have it really insert a tab?
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

just untested idea (should work though) - i would go for a onkeydown event, find out the code of the tab key, and then test the event for it. If tab was pressed, insert \t and return false.
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

Ok, here it is:

Code: Select all

<textarea id='tarea' onkeydown='if (event.keyCode == 9) {this.value += "\t"; return false;}'></textarea>
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

WORKS LIKE A FLIPPIN CHARM! Thanks a $#!t load dude! :D

EDIT: One Problem though. When I try to insert a tab within the middle of text it just adds the tab at the end. Is there a way of getting around this one?

Maybe if we we could get the current selection of the textarea. Or something simular.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Take a look at this page: http://weblogs.asp.net/skillet/archive/ ... 95838.aspx ... i didnt test it out, but it may be a start...
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Take a look at this page: http://weblogs.asp.net/skillet/archive/ ... 95838.aspx ... i didnt test it out, but it may be a start
is IE6-only code, doesn't work in FF. But might be a start, indeed
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

I don't get it? Is there an example of what scott is trying to do (in the link)?
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Is there a way to find out where the clients character position was in the textarea?
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post by thiscatis »

using the this.select and a character count?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

this article should help:
http://www.webreference.com/programming/javascript/ncz/

uses createTextRange() for IE
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

I'm really having a hard time following this artical. I don't know why. What property or method would I use on the textarea to get the character position of the client?

element.property/method
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Moz and IE have different (both proprietary) systems when it comes to this.

This helped me when I needed it:
http://www.faqts.com/knowledge_base/vie ... /aid/13562
Post Reply