...

Prevent tab

Bosko
09-27-2002, 03:07 PM
What I need to do is add a tab character,\t, to a textarea when the user presses the Tab-key,instead of moving the focus to the next element.Fortunaly Konqueror already does this,so I only need to worry about Mozilla.Currently I have this code,which adds a tab character to the textarea,but the focus still moves to the address bar/body when I press the Tab-key:

<form>
<textarea id="textbox">arf!</textarea>
</form>

<script type="text/javascript">
function CatchTab(e){
if(e.keyCode==9){
document.getElementById("textbox").value=document.getElementById("textbox").value+"\t";
e.preventDefault();
e.stopPropagation();
}
document.getElementById("textbox").focus();
}

document.getElementById("textbox").addEventListener("keydown",CatchTab,true);
</script>

How can I prevent Mozilla from moving the focus to another element?

Thanks in advance for any help.

jkd
09-27-2002, 03:49 PM
If you eventInstance.stopPropagation() on the event before it reaches the textarea, it won't focus the next field.

aCcodeMonkey
09-27-2002, 07:40 PM
Bosko,

Heres how I do it..

First add an onLoad statement in the body tag.

Example:
<BODY onLoad="addListener()">

Next add the function to your script in the header section.

Example:
function addListener(){document.getElementById('txtTextArea').addEventListener('keypress', InsertTab, true);}

Sample Code:

<script language="javascript">
function addListener(){document.getElementById('txtTextArea').addEventListener('keypress', InsertTab, true);}
function InsertTab(e){
if (e.keyCode==9){
e.preventDefault()
sText=new String(document.getElementById('txtTextArea').value+'\t')
document.getElementById('txtTextArea').value=sText
e.stopPropagation();
}
}
</script>


Hope this helps :cool:

Bosko
09-27-2002, 07:57 PM
Okay,thanks :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum