Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Prevent tab

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Netherlands
    Posts
    217
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Prevent tab

    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:
    Code:
    <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.

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    If you eventInstance.stopPropagation() on the event before it reaches the textarea, it won't focus the next field.

  • #3
    New Coder
    Join Date
    Sep 2002
    Posts
    82
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    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
    Attached Files Attached Files
    a credit card for a newbie from Amex: The Amex Blue card would like to start developing a credit history.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Netherlands
    Posts
    217
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay,thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •