Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all

For tabbing i have written below code
JavaScript
function bindTabbing(event)
{
    if (event.which || event.keyCode)
    {
        // 9 indicates TAB key.
        if ((event.which == 9) || (event.keyCode == 9))
        {
            var subject = document.getElementById(txtProgramEditor); // Get the Editor textarea control.
            if (event.srcElement == subject) // If tab is pressed in the textarea then do the action.
            {
                // Creates a range and adds the Tab equivalent character in the text range.
                subject.selection = document.selection.createRange();
                subject.selection.text = String.fromCharCode(9);
                event.returnValue = false;
            }

            if (subject.selectionStart || subject.selectionStart == '0')
            {
                var str = subject.value;
                var startIndex = subject.selectionStart;
                subject.value = str.substring(0, startIndex) + '    ' + str.substring(startIndex, str.length);
                subject.focus();
                subject.selectionEnd = startIndex + 4;
                return false;
            }
        }
    }
    else
    {
        return true;
    }
}

this is working fine in FireFox and IE but not working in Chrome and Safari

Please locate the problem done by me or suggest what to do..

Thanks in advance.
Posted
Updated 17-Sep-12 1:53am
v2

1 solution

createRange does not work in safari and Chrome
JavaScript
subject.selection = document.selection.createRange();

rather then this use
JavaScript
subject.selection = window.getSelection();


This will work in all browser and IE 9+.


refer these links
http://help.dottoro.com/ljrvjsfe.php[^]
http://stackoverflow.com/questions/7021497/document-selection-createrange-not-working-in-chrome-and-safari[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900