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 994121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:31:52+00:00 2026-05-16T06:31:52+00:00

I’m attempting to make an auto-indenting textarea and so far it works with the

  • 0

I’m attempting to make an auto-indenting textarea and so far it works with the following code. The problem that I’m having is that I’m currently preventing the default action of pressing enter in order to calculate the tabs in the line, and then inserting the newline afterwards.

This works, but I want to have the default action of the textarea, in that if you hold enter down, it doesn’t scroll the textarea until the caret hits the bottom row, and then the scroll bar stays with caret at the bottom row. The current code below keeps the caret in field of vision if used anywhere in the textarea, but it causes content above the caret to scroll up, which is a compromise as the caret no longer disappears. If there is no other solution this will work fine, but I’m hoping there are other ideas.

    var start = this.selectionStart-1;
    var end = this.selectionEnd;
    var sT = this.scrollTop;
    var sH = this.scrollHeight;
    var vH = $(this).height();

    var x = (sH - sT) - vH;

    // Check if hitting enter on the first line as no newline will be found
    if (this.value.lastIndexOf('\n',start)==-1)
     var startSubstr = 0;
    else 
     var startSubstr = this.value.lastIndexOf('\n',start)+1;

    var endFirst = end - startSubstr;

    var valueToScan = this.value.substr(startSubstr,endFirst);
    var count = 0;

    // Count only \t at the beginning of the line, none in the code   
    while (valueToScan.indexOf('\t')!=-1) {
     if (valueToScan.indexOf('\t')!=0)
      break;
     count++;
     valueToScan = valueToScan.substr(valueToScan.indexOf('\t')+1);
    }

    // Command to isert the newline, and then add the \t's found in previously  
    $(this).insertAtCaret('\n');
    for (var i=0;i<count;count--) {
     $(this).insertAtCaret('\t');
    }  

    var sH = this.scrollHeight;
    this.scrollTop = (sH - x) - vH;

EDIT
As an update, to clear away any confusion, I’m attempting to create an IDE style textbox, so for example in most IDEs if you type the following

function example(whatever) {
     Example Stuff // And then hit enter here

I want it to take you to the same tab distance from the left border as “Example Stuff” in order to keep your code easier to read. I currently have that functionality but the problem is that I am intercepting the enter key, which means I have to then provide that functionality. I’m having difficulty replicating the exact functionality of not scrolling the textbox until the cursor hits the bottom border. So, if you were to hold enter at the top of the textarea, the cursor just scrolls down the textarea, moving any text with it until it hits the bottom border, and then the scrollbar for the textarea will follow the cursor. Make sense?

EDIT 2 Updated tags to indicate code is with PHP

I’ve been working on this while this post has been sitting. I think another option that I’m going to explore when I get a chance is rather than preventing enter just allow the default action of hitting enter and read the string from lastIndexOf('\n')-1 to the most recent \nfor any \t. I think that would give me the string I’m looking for, but without messing up the behavior of the textarea. Will update again after testing.

EDIT 3 Resolved, as per my previous update I decided to scan the string after the key is pressed, preserving the natural behavior (section changed added below for those interested) and the only problem that really remains is that if you hold enter, it won’t count the indentation from the origin of the enter press, which is fine by me. I’m going to give this one to http://stackoverflow.com/users/15066/matyr&#8217;>matyr, although I did figure out before he responded he was still the closest.

if (this.value.lastIndexOf('\n',start-2)==-1) {
 var startSubstr = 0;
} else {
 var startSubstr = this.value.lastIndexOf('\n',start-1)+1;
}    
var valueToScan = this.value.substr(startSubstr,start);
  • 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-05-16T06:31:52+00:00Added an answer on May 16, 2026 at 6:31 am

    I want to have the default action of the textarea

    How about binding to keyup instead of keydown/keypress and insert the tab after native line break?

    the scroll bar stays with caret at the bottom row

    You can preserve the scroll position by restoring scrollTop.

    <!DOCTYPE html>
    <title>autoindent example</title>
    <textarea id="TA" rows="8"></textarea>
    <script>
    document.getElementById('TA').addEventListener('keyup', function(v){
      if(v.keyCode != 13 || v.shiftKey || v.ctrlKey || v.altKey || v.metaKey)
        return;
      var val = this.value, pos = this.selectionStart;
      var line = val.slice(val.lastIndexOf('\n', pos - 2) + 1, pos - 1);
      var indent = /^\s*/.exec(line)[0];
      if(!indent) return;
      var st = this.scrollTop;
      this.value = val.slice(0, pos) + indent + val.slice(this.selectionEnd);
      this.selectionStart = this.selectionEnd = pos + indent.length;
      this.scrollTop = st;
    }, false);
    </script>
    <p>(not considering IE here)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.