Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 05-11-2009, 07:09 PM Override tab
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I'm trying to prevent the tab key for removing focus from a text field and instead insert the tab character. So far I've been able to insert the tab character when tab is pressed, but the text field still loses focus. This is what I have so far:

Code:
function tabKeyDown(obj)
{
    if (event.keyCode == 9 && event.srcElement == obj)
    {
        obj.selection = document.selection.createRange();
        obj.selection.text = String.fromCharCode(tabKeyCode);
        //What now?
    }
}
What can I do to either prevent focus from being lost, or return focus to the text area (and preferably return the cursor to the correct position).
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
 
Register now for full access!
Old 05-11-2009, 08:20 PM Re: Override tab
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
You should be able to use the focus method: http://www.w3schools.com/HTMLDOM/met_text_focus.asp . It works on any text field or for input.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 05-11-2009, 08:48 PM Re: Override tab
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by wayfarer07 View Post
You should be able to use the focus method: http://www.w3schools.com/HTMLDOM/met_text_focus.asp . It works on any text field or for input.
I already tried
Code:
function tabKeyDown(obj)
{
    if (event.keyCode == 9 && event.srcElement == obj)
    {
        obj.selection = document.selection.createRange();
        obj.selection.text = String.fromCharCode(tabKeyCode);

        obj.focus();
    }
}
but that didn't work. I'm thinking this is because the function is triggered before the tab key event moves focus to the next field. I'm using onkeydown to call the function (I'll try right now with onkeyup with onkeyup it doesn't work at all).

Also, if I call focus() on the text field, where will the cursor be set?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 05-11-2009 at 08:52 PM..
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 05-11-2009, 10:19 PM Re: Override tab
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Focus will cause the cursor to be set on the text field that it has been called on, at the end of any text that exists in that field. Focus is naturally brought on or removed by either tabbing to or clicking on various fields.

Try returning false from the tab key function, to see if that stops the default action from happening. Probably won't work, but it's worth a shot. This works for submit and click events, though I'm not sure about key events.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 05-11-2009, 10:27 PM Re: Override tab
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Returning false didn't work either.

I can hack something together that would cause focus to return to the text field, but I would prefer it if I could prevent focus from being removed from the field, as I need to cursor to remain where it is (ie if someone hits tab in the middle of some text I want a tab to be inserted and I don't want the cursor to jump to the end of the text).

Thanks for trying.
EDIT: Remind me to give you some TP later.... apparently I need to spread it around some more.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 05-11-2009, 10:38 PM Re: Override tab
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Hmmm.... I'm not sure about moving the cursor, I've never actually tried to do that. There may be a way to do it in some browsers, but I've never investigated.

It brings up interesting questions. I'm sure you'll figure out something. It sounds like stopping the default action is definitely the thing to do.

I was browsing around while I was writing this reply, and found this: http://www.dynamicdrive.com/forums/s...ad.php?t=44468 Maybe it will help, though I barely glanced at it Off to bed for me...
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.

Last edited by wayfarer07; 05-11-2009 at 10:40 PM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 05-11-2009, 10:43 PM Re: Override tab
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I figured it out:
Code:
<script language="javascript">
function addListener(){document.getElementById('content').addEventListener('keypress', InsertTab, true);}
function InsertTab(e){
	if (e.keyCode==9){
		e.preventDefault()
		obj = document.getElementById('content');
		obj.selection = document.selection.createRange();
                obj.selection.text = String.fromCharCode(9);
		e.stopPropagation();
	}
}
</script>
and in the <body> tag add onload="addlistener()"
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 05-11-2009 at 10:47 PM..
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 05-12-2009, 10:23 AM Re: Override tab
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Not going to work for IE though, since it doesn't use the WC3 event model (even IE8 doesn't). You could jQuery it, or use any framework, or try to follow the original event model: otherwise you'll need to make an alternative script for the Microsoft model.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 05-12-2009, 11:16 AM Re: Override tab
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Actually as far as I can tell it ONLY works in opera (which is fine for me because that is what I use). Is there anything in particular that makes it incompatible with firefox?

EDIT:
I forgot to mention... the tabs are appearing just fine in firefox. My problem is that focus is still lost. Is there a different way I'm supposed to use preventDefault in firefox?

Also,

I've never used jquery, how would I go about doing this using jquery?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 05-12-2009 at 11:53 AM..
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 05-12-2009, 11:48 AM Re: Override tab
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Haven't found a solution, but here is my debugging code:
PHP Code:
<script language="javascript">
function 
addListener(){document.getElementById('content').addEventListener('keypress'InsertTabtrue);}
function 
InsertTab(e){
    if (
e.keyCode==9){
        
e.preventDefault();
        
obj document.getElementById('content');
        var 
userSelection;
        if (
window.getSelection) {
            
userSelection window.getSelection();
        }
        else if (
document.selection) {
            
userSelection document.selection.createRange();
        }
        
alert(userSelection);
        
obj.selection userSelection;
        
obj.selection.text String.fromCharCode(9);
        
e.stopPropagation();
    }
}
addListener();
</script> 
For some reason there is no content to the var userSelection
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 05-12-2009, 12:01 PM Re: Override tab
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Here's an article I found an Ajaxian about this: http://ajaxian.com/archives/handling-tabs-in-textareas maybe that will help.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 05-12-2009, 12:27 PM Re: Override tab
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
The code in the article works very well in firefox, but is a little buggy in opera. I added a check to execute that script if the browser if firefox and mine otherwise. Still no dice in chrome.

Thanks for the help, now I need to install IE to figure out which to use in that case. Reinstalling IE makes me feel dirty.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 05-12-2009, 12:30 PM Re: Override tab
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
It doesn't work in IE, I just checked. Read the comments... someone claims to have made a version that works.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 05-12-2009, 01:59 PM Re: Override tab
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
It doesn't. Or I think it doesn't. The function took the object itself as a parameter. (ie catchTab(obj, e) ). I can't call the function like this so I did this:

Code:
catchTab(e)
{
     var obj = document.getElementById('content');
.
.
.
That should have the same effect correct?

Anyways... I have it working well in firefox and opera, which are the two browsers I care most about. If I could find a function that accomplishes this in IE (even if it only worked in IE) I would be happy.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to Override tab
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.38170 seconds with 12 queries