Return key pressed inside a text box must maintain no of TABs in the previous line

The attached code changes the behavior of the TAB key inside a text box, so that pressing the TAB key produces an indent, as opposed to changing focus. (I have added the following attribute to the textbox:
onkeydown="return interceptTabs(event, this);")

Now, when I hit the return key in a text box, the indent should be set to the same number of TABs as to what is now in the previous line.

Please tell me how this can be done.
function insertAtCursor(myField, myValue) {
        //IE support
        if (document.selection) {
            myField.focus();
            sel = document.selection.createRange();
            sel.text = myValue;
        }

        //MOZILLA/NETSCAPE support
        else if (myField.selectionStart || myField.selectionStart == '0') {
            var startPos = myField.selectionStart;
            var endPos = myField.selectionEnd;
            restoreTop = myField.scrollTop;

            myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);

            myField.selectionStart = startPos + myValue.length;
            myField.selectionEnd = startPos + myValue.length;

            if (restoreTop > 0) {
                myField.scrollTop = restoreTop;
            }
        }
        else {
            myField.value += myValue;
        }
    }

    function interceptTabs(evt, control) {
        key = evt.keyCode ? evt.keyCode : evt.which ? evt.which : evt.charCode;
        if (key == 9) {
            insertAtCursor(control, '\t');
            return false;
        }
        else {
            return key;
        }
    }

Open in new window

dshrenikAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dshrenikAuthor Commented:
One approach could be to figure out the cursor position at the time of pressing the Return key, and count the number of TABs at the beginning of the line, and insert the same number after the line.
dshrenikAuthor Commented:
Is something like this implementable?
dshrenikAuthor Commented:
I figured it out. I have attached the code.
function insertAtCursor(myField, myValue) {
        //IE support
        if (document.selection) {
            myField.focus();
            sel = document.selection.createRange();
            sel.text = myValue;
        }

        //MOZILLA/NETSCAPE support
        else if (myField.selectionStart || myField.selectionStart == '0') {
            var startPos = myField.selectionStart;
            var endPos = myField.selectionEnd;
            restoreTop = myField.scrollTop;

            myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);

            myField.selectionStart = startPos + myValue.length;
            myField.selectionEnd = startPos + myValue.length;

            if (restoreTop > 0) {
                myField.scrollTop = restoreTop;
            }
        }
        else {
            myField.value += myValue;
        }
    }

    function insertTabs(myField) {
        //IE support
        if (document.selection) {
            myField.focus();
            sel = document.selection.createRange();
            sel.text = myValue;
        }

        //MOZILLA/NETSCAPE support
        else if (myField.selectionStart || myField.selectionStart == '0') {
            var startPos = myField.selectionStart;
            var endPos = myField.selectionEnd;
            restoreTop = myField.scrollTop;

            //Calculating number of TABs
            var currentPosition = myField.value.substring(0, startPos)
            var lines = currentPosition.split("\n");
            var currentLine = lines[lines.length - 1];

            var tabCount = 0;
            for (i = 0; i < currentLine.length; i++) {
                if (currentLine[i] == "\t")
                    tabCount++;
                else
                    break;
            }

            var myValue = "\n";
            for (i=0; i<tabCount; i=i+1) {
                myValue += "\t";
            }

            myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);

            myField.selectionStart = startPos + myValue.length;
            myField.selectionEnd = startPos + myValue.length;

            if (restoreTop > 0) {
                myField.scrollTop = restoreTop;
            }
        }
        else {
            myField.value += myValue;
        }
    }

    function interceptTabs(evt, control) {
        key = evt.keyCode ? evt.keyCode : evt.which ? evt.which : evt.charCode;
        if (key == 9) {
            insertAtCursor(control, '\t');
            return false;
        }
        else if (key == 13) {
            insertTabs(control);
            return false;
        }
        else {
            return key;
        }
    }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Terry WoodsIT GuruCommented:
Nice work... thanks for sharing!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.