www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 04-10-2004, 07:57 PM
    crh3675's Avatar
    crh3675 crh3675 is offline
    Web Demystifier
     
    Join Date: Jan 2004
    Location: Washington DC
    Posts: 1,105
    Lightbulb How to enable TAB key in TEXTAREA

    Here is the code that will allow your users to use a TAB key in a textarea.

    Enjoy!

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function setSelectionRange(input, selectionStart, selectionEnd) {
      if (input.setSelectionRange) {
        input.focus();
        input.setSelectionRange(selectionStart, selectionEnd);
      }
      else if (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
      }
    }
    
    function replaceSelection (input, replaceString) {
    	if (input.setSelectionRange) {
    		var selectionStart = input.selectionStart;
    		var selectionEnd = input.selectionEnd;
    		input.value = input.value.substring(0, selectionStart)+ replaceString + input.value.substring(selectionEnd);
        
    		if (selectionStart != selectionEnd){ 
    			setSelectionRange(input, selectionStart, selectionStart + 	replaceString.length);
    		}else{
    			setSelectionRange(input, selectionStart + replaceString.length, selectionStart + replaceString.length);
    		}
    
    	}else if (document.selection) {
    		var range = document.selection.createRange();
    
    		if (range.parentElement() == input) {
    			var isCollapsed = range.text == '';
    			range.text = replaceString;
    
    			 if (!isCollapsed)  {
    				range.moveStart('character', -replaceString.length);
    				range.select();
    			}
    		}
    	}
    }
    
    
    // We are going to catch the TAB key so that we can use it, Hooray!
    function catchTab(item,e){
    	if(navigator.userAgent.match("Gecko")){
    		c=e.which;
    	}else{
    		c=e.keyCode;
    	}
    	if(c==9){
    		replaceSelection(item,String.fromCharCode(9));
    		setTimeout("document.getElementById('"+item.id+"').focus();",0);	
    		return false;
    	}
    		    
    }
    </script>
    </head>
    <body>
    <form>
    <textarea name="data" id="data" rows="20" columns="35" wrap="off" onkeydown="return catchTab(this,event)" ></textarea>
    <input type="submit" name="submit" value="Submit"/>
    </form>
    __________________
    Bittersweet web development.
    Reply With Quote
      #2  
    Old 04-11-2004, 10:51 AM
    steelersfan88 steelersfan88 is offline
    Banned
     
    Join Date: Jun 2003
    Location: The United States Site: http://kellyj.t35.com
    Posts: 2,718
    Thanks for the code. We, however, normally do not post code like this until a user asks for it. Then, any subsequent user will get a link to where the question is already answered Nice script, however.
    Reply With Quote
      #3  
    Old 04-11-2004, 10:53 AM
    crh3675's Avatar
    crh3675 crh3675 is offline
    Web Demystifier
     
    Join Date: Jan 2004
    Location: Washington DC
    Posts: 1,105
    OK then,
    is it possible to allow users the ability to use the TAB key in a textarea? I can't find an answer. Please help!
    __________________
    Bittersweet web development.
    Reply With Quote
      #4  
    Old 04-11-2004, 10:54 AM
    crh3675's Avatar
    crh3675 crh3675 is offline
    Web Demystifier
     
    Join Date: Jan 2004
    Location: Washington DC
    Posts: 1,105
    Sure you can, check out the script I provided above your post.
    __________________
    Bittersweet web development.
    Reply With Quote
      #5  
    Old 04-11-2004, 10:55 AM
    crh3675's Avatar
    crh3675 crh3675 is offline
    Web Demystifier
     
    Join Date: Jan 2004
    Location: Washington DC
    Posts: 1,105
    Thanks man! Works great!
    __________________
    Bittersweet web development.
    Reply With Quote
      #6  
    Old 04-11-2004, 11:14 AM
    steelersfan88 steelersfan88 is offline
    Banned
     
    Join Date: Jun 2003
    Location: The United States Site: http://kellyj.t35.com
    Posts: 2,718
    nice crh3675. We also don't talk to ourself, but we can pretend anyway
    Reply With Quote
      #7  
    Old 04-11-2004, 11:23 AM
    crh3675's Avatar
    crh3675 crh3675 is offline
    Web Demystifier
     
    Join Date: Jan 2004
    Location: Washington DC
    Posts: 1,105
    What would be nice is a snippet repository in the forum for people to search. Many times people ask the same questions and we redirect them to different posts.

    When I come up with a solution for development, it is usually for something that more than just "I" need to use so I like to post the code. I'll refrain from talking to myself but it sure gets lonely
    __________________
    Bittersweet web development.
    Reply With Quote
      #8  
    Old 04-11-2004, 11:31 AM
    steelersfan88 steelersfan88 is offline
    Banned
     
    Join Date: Jun 2003
    Location: The United States Site: http://kellyj.t35.com
    Posts: 2,718
    well the weekends get pretty boring, so talking to yourself is ok, i guess
    Reply With Quote
      #9  
    Old 04-11-2004, 02:46 PM
    AdamGundry's Avatar
    AdamGundry AdamGundry is offline
    Not quite a mathematician
     
    Join Date: Nov 2002
    Location: Dartmoor [Holiday]
    Posts: 2,406
    If you've got a useful general-purpose script, you might consider submitting it to webdevfaqs.com, a site some of the mods set up to deal with issues such as these.

    It's then easier to find than trawling through a forum search, and (someday) people might actually start checking it _before_ asking that question about popups for the 100th time.

    Nice script, anyway.

    Adam
    __________________
    "If you’re not using valid HTML, then you haven’t created a Web page. You may have created something else, but it isn’t a Web page." - Joe Clark

    Do something amazing (USA) | Make Poverty History
    Reply With Quote
      #10  
    Old 04-11-2004, 03:35 PM
    nshiell's Avatar
    nshiell nshiell is offline
    King Of The Universe
     
    Join Date: Feb 2004
    Location: London UK
    Posts: 249
    Woo

    Iv'e just run that script and it works great.
    V usefull for a project im working on ill post a link to it when im done
    __________________

    Web Standards Make Sense!
    Reply With Quote
      #11  
    Old 04-11-2004, 03:49 PM
    steelersfan88 steelersfan88 is offline
    Banned
     
    Join Date: Jun 2003
    Location: The United States Site: http://kellyj.t35.com
    Posts: 2,718
    see, it was all because you talked to yourself
    Reply With Quote
      #12  
    Old 03-04-2006, 06:06 AM
    meethimanshu meethimanshu is offline
    Registered User
     
    Join Date: Mar 2006
    Posts: 1
    Lightbulb Fantastic job done

    What a brilliant script. You saved my life. Thanks a ton for that Hurrraayyy!
    Reply With Quote
      #13  
    Old 03-04-2006, 03:35 PM
    felgall's Avatar
    felgall felgall is offline
    Computer Consultant
     
    Join Date: Mar 2005
    Location: Sydney, Australia
    Posts: 7,982
    Of course that will override the standard function of the tab key in the browser and totally confuse your visitors.

    TAB key = move to next field.

    Total chaos for visitors who don't use a mouse.

    Also it won't work in a lot of browsers because the following code is garbage

    Code:
    if(navigator.userAgent.match("Gecko")){
    c=e.which;
    }else{
    c=e.keyCode;
    }
    If you are going to test for what key is pressed then do it properly using feature sensing not browser sensing.

    c = e.which ? ewhich : e.keyCode;
    __________________
    Stephen

    Last edited by felgall; 03-04-2006 at 03:42 PM.
    Reply With Quote
      #14  
    Old 03-05-2006, 07:35 AM
    crh3675's Avatar
    crh3675 crh3675 is offline
    Web Demystifier
     
    Join Date: Jan 2004
    Location: Washington DC
    Posts: 1,105
    Quote:
    Also it won't work in a lot of browsers because the following code is garbage
    Thanks for the kind words felgall. Next time try to be a bit ore positive and say something like:

    Quote:
    Here is a better way to check for the target element since some browsers won't recognize this code:
    Code:
    if(navigator.userAgent.match("Gecko")){
    c=e.which;
    }else{
    c=e.keyCode;
    }
    New Code
    Code:
    c = e.which ? ewhich : e.keyCode;

    Quote:
    Of course that will override the standard function of the tab key in the browser and totally confuse your visitors.
    Felgall, You also need to understand that client requirements are all different and you are not the one who determines what a client wants. Maybe it's for a web application that has a single textarea field. So before acting like a know-it-all, you need to understand the programmer/client's usage. This script helped me with a project and I hope it can help others. If you have something better or more to add, you are welcome to offer input.
    __________________
    Bittersweet web development.

    Last edited by crh3675; 03-05-2006 at 07:39 AM.
    Reply With Quote
      #15  
    Old 03-05-2006, 02:17 PM
    felgall's Avatar
    felgall felgall is offline
    Computer Consultant
     
    Join Date: Mar 2005
    Location: Sydney, Australia
    Posts: 7,982
    People visiting web pages expect them to work a certain way. Tab is used to move from one field to the next. Changing the tab functionality may make your page unusable to some visitors as they will have no way to move between fields as you have changed that function to do something different. At best that person will then leave your site. At worst they will sue you for making the site inaccessible to them.
    __________________
    Stephen
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools
    Display Modes Rate This Thread
    Rate This Thread:

    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 Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:15 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.