In Javascript, I’d like determine whether an element, say an A element, exists inside a given range/textRange. The aim is to determine if the user’s current selection contains a link. I am building a rich text editor control.
The range object has a commonAncestorContainer (W3C) or parentElement() (Microsoft) method which returns the closest common anscestor of all elements in the range. However, looking inside this element for A elements won’t work, since this common ancestor may also contain elements that aren’t in the range, since the range can start or end part way through a parent.
How would you achieve this?
I ended up going with a solution like this:
Where getrange() is another function of mine to get the current selection as a range object.
To use, call it like
Where editor is the contenteditable element, or body in a designmode iframe.