Page 1 of 1

[SOLVED] Get selection range "parent node" in IE

Posted: Mon Mar 07, 2005 8:40 am
by CoderGoblin
I have a problem with cross browser combatibility which I have not found a solution to...

For Netscape/Mozilla you can get the "parent node" of a selection using the following code...

Code: Select all

var selection = window.getSelection();
rng = selection.getRangeAt(selection.rangeCount - 1).cloneRange(); parentnode=rng.commonAncestorContainer;
Unfortunately IE6 does not support/allow the rng.commonAncestorContainer command and of course getting the selection is different.

Code: Select all

var selection = document.selection;
 if (selection != null) { rng = selection.createRange(); }
 // Need the equivalent to rng.commonAncestorContainer Here
Does anyone have any ideas on how to get the "common Ancestor Node" of the Range using IE 6 ?

Posted: Mon Mar 07, 2005 9:01 am
by n00b Saibot
getParentNode() will help you get Ancestor in IE's domain ;)

Posted: Mon Mar 07, 2005 9:38 am
by CoderGoblin
If I had the node within the possible selection (bear in mind this could start in the middle of one node and spread across multiple levels) I agree getParentNode() may have been of help. Unfortunately IE selection.createRange seems to return just a text range, not linked to the document itself. I am currently investigating this further but if you have any examples using the IE selection model I would be interested....

Re: Get selection range "parent node" in IE

Posted: Mon Mar 07, 2005 10:15 am
by n00b Saibot

Code: Select all

var selection = window.getSelection();
rng = selection.createRange();
parentnode=rng.parentElement();
alert(parentnode.tagName);
HIH ;)

If there are any problems regarding CSS, JS, VBS etc just ask, OK ;)

ps: and heck, the rest of the lot too :lol:

Posted: Tue Mar 08, 2005 3:04 am
by CoderGoblin
Thanks, that worked