Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:57:08+00:00 2026-06-12T23:57:08+00:00

Possible Duplicate: How to handle <tab> in textarea? I need a way to maintain

  • 0

Possible Duplicate:
How to handle <tab> in textarea?

I need a way to maintain tab levels in a textarea, so far this is what I have used:

<script type="text/javascript">
   $("textarea").keydown(function(e) {
      var $this, end, start;
      if (e.keyCode === 9) {
         start = this.selectionStart;
         end = this.selectionEnd;
         $this = $(this);
         $this.val(
            $this.val().substring(0, start)
            + "\t" + $this.val().substring(end)
         );
         this.selectionStart = this.selectionEnd = start + 1;
         return false;
      }
   });
</script>

But this only inserts tabs (I found this on another SO post so I don’t really know what it does as I’m not a JS expert )

Is there a way for those tabs to be there when I hit the return key? I need this because I am making a very simple code editor for my admin panel (no need for syntax highlight or anything) and it uses TextAreas.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:57:09+00:00Added an answer on June 12, 2026 at 11:57 pm

    I definitely don’t claim this to be a work of art, I threw it together in a matter of minutes. It will need some refining but should get you going in the right direction.

    Global Variables

    var keyPressed = null;
    var timesPressed = 0;
    var tabString = '';
    var lastKeyPressed = null;
    

    Edit — Added an important step to see if the user wants to break the tab set.

    Dom ready functions

    $(function(){
        $("textarea").keydown(function(e) {
            var $this, end, start;
            if (e.keyCode === 9 || e.which === 9) {
                start = this.selectionStart;
                end = this.selectionEnd;
                $this = $(this);
                $this.val(
                   $this.val().substring(0, start)
                   + "\t" + $this.val().substring(end)
                );
                this.selectionStart = this.selectionEnd = start + 1;
                keyPressed = e.keyCode || e.which;
                timesPressed = timesPressed +1;
                return false;
            }else if((e.keyCode === 13 || e.which === 13) && keyPressed === 9){
                start = this.selectionStart;
                end = this.selectionEnd;
                $this = $(this);
                tabString += '\t';
                $this.val(
                $this.val().substring(0, start)
                    + "\r"+tabString + $this.val().substring(end)
                );
                this.selectionStart = this.selectionEnd = start + timesPressed +1;
                lastKeyPressed = e.keyCode;
                return false;          
            }else if((e.keyCode === 8 || e.which === 8) && lastKeyPressed === 9){
                keyPressed = null;   
                timesPressed = 0; 
                tabString = '';     
            }
        }); 
    });​
    

    Here’s a working jsFiddle

    I didn’t really have a good chance to test it too much. Let me know if it’s too flaky and I’ll go back to reworking it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: handle json request in PHP The following: $.ajax({ type: 'POST', url: 'receive-json.php',
Possible Duplicate: Hiding textarea resize handle in Safari in textarea input the user can
Possible Duplicate: How to create a type bounded within a certain range I have
Possible Duplicate: Handle URL anchor change event in js Is it possible to have
Possible Duplicate: MVC ajax calls - where to handle them? I have a framework
Possible Duplicate: How does .net managed memory handle value types inside objects? I have
Possible Duplicate: How to use java.net.URLConnection to fire and handle HTTP requests? I need
Possible Duplicate: Open/handle Word documents like SharePoint I´ve developed one website where it is
Possible Duplicate: Java: Global Exception Handler I thought of something like this: public static
Possible Duplicate: Java Full Screen Program (Swing) -Tab/ALT F4 I've got a full screen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.