Search code examples
javascriptfirefoxselectionmozillaspaces

How can you move the cursor in front of inserted text in a custom textbox in Mozilla?


I'm using Firefox 3.6 (must be Firefox 3.6, doesn't need to be cross-browser compatible) and the selection object returned from window.getSelection(). The code below is capturing the tab key and preventing it from tabbing out of a custom text box. It is then inserting 5 non-breaking spaces in front of the cursor. How can I make the cursor jump to the end of the inserted non-breaking spaces?

content.bind('keydown', function(evt) {
    var TABKEY = 9;
    if (evt.keyCode == TABKEY) {
      var TAB_SPACES = 5;
      evt.preventDefault();
      var sel = window.getSelection();
      var range = sel.getRangeAt(0);
      range.insertNode(document.createTextNode('\u00a0'.times(TAB_SPACES)));
    }
  }, false
);

Solution

  • When you say a custom text box, this is done using contenteditable? Try this:

    var text = document.createTextNode('\u00a0'.times(TAB_SPACES));
    range.insertNode(text);
    range.setStartAfter(text);