On browsers other than IE <= 8, this will do the following:
function placeCaretAfterNode(node) { if (typeof window.getSelection != "undefined") { var range = document.createRange(); range.setStartAfter(node); range.collapse(true); var selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range); } }
However, some browsers (in particular, WebKit-based) have a fixed view of which positions in the document are valid for the caret, and normalize any range that you add to the selection to fit these ideas. The following example will do what you want in Firefox and IE 9, but not in Chrome or Safari:
http://jsfiddle.net/5dxwx/
None of the workarounds are good. Options include:
- add a zero space character after the range and select it
- use the
<a> element instead of the <span> because WebKit throws an exception for <a> elements
Tim down
source share