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

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

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

Post 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 ?
Last edited by CoderGoblin on Tue Mar 08, 2005 3:04 am, edited 1 time in total.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

getParentNode() will help you get Ancestor in IE's domain ;)
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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....
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Re: Get selection range "parent node" in IE

Post 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:
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Thanks, that worked
Post Reply