1

How do I use the execCommand() in Chrome? Here is the code I have right now It is being used to insert a special character when hitting the tab button

function editAble(supr){
    document.getElementById('codeline').contentEditable='true';
    document.getElementById('codeline').onkeydown=function(e)
        {

        if(e.keyCode==9){
            e.preventDefault();
            range1 = document.getElementById('codeline');
            range1.execCommand("InsertHtml",false,"p");

        }
    }
}
comu
  • 921
  • 1
  • 11
  • 29

1 Answers1

2

The execCommand() method is a method of Document objects, not elements. IE also provides execCommand() as a method of its TextRange and ControlRange objects, but these are not present in other browsers.

document.execCommand("InsertHtml", false, "p");

You may want to consider what happens if the user presses the Tab key when the user has previously selected some text: in that case you'd probably want to delete the contents of the selection before inserting your tab character.

Community
  • 1
  • 1
Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • I understand what you mean, I just have 0 idea how to approach it correctly. I feel like what I have should work, but it just... doesn't. Also, in every other browser but chrome, if I have the contentEditable area set to a pre, than the Tabs work without JavaScript – comu Oct 09 '11 at 16:14