I'm trying to use JavaScript to wrap selected elements in a new div. Using the following HTML as an example:

HTML Code:
<html>
<body>
<p id="p1">Paragraph 1</p>
<p id="p2">Paragraph 2</p>
<p id="p3">Paragraph 3</p>
<p id="p4">Paragraph 4</p>
</body>
</html>
With Firefox I can get the selection as a range, which has the properties startContainer and endContainer.

Say that the selection runs from 'Paragraph 2' to 'Paragraph 4'. The value of startContainer will be the 'Paragraph 2' text node, and I can work back to get the parent paragraph element node (call it 'startNode'). Similarly, I can work back from endContainer to get a paragraph element node (call it 'endNode').

I can then take startNode, endNode and the elements in between, add them to a new div, and put the new div back.

The problem is that I can't find an equivalent for IE. I can't find a way of translating the start and end of the selection to positions in the DOM.

Has anyone else come across a solution? I can't really post my full code as it's part of an unfinished Xinha plugin, but I can post the section that I've written if it makes the problem clearer.

Hope that makes sense, and thanks to everyone that tries to help