I've looked around for a while, but there doesn't seem to be a simple way to do this. jQuery doesn't help the in least, it seems to entirely lack any support for selection or DOM ranges. Something which I hoped would be as simple as $.selection.filter('img')
seems to only be doable with dozens of lines of code dealing with manually enumerating elements in ranges and browser implementation inconsistencies (though ierange helps here). Any other shortcuts?
var fragment = getSelection().getRangeAt(0).extractContents();
The nodes in the selection will be removed and returned in a DocumentFragment
, and you can now access the childNodes
of fragment
just like you would any element.
it seems to entirely lack any support for selection or DOM ranges
Yeah, the reason for that is IE lacks support for selection and DOM Range. You can build an abstraction layer on top of IE's non-standard ‘TextRange’ objects, but due to the extremely poor interface exposed by TextRanges it's difficult, slow and complicated enough that it's a full-on library in itself. See eg. this one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With