Change selection in Safari via JavaScript
It’s easy to get the current selection with Javascript. It’s easy to select an entire node.
But what if you want to create a selection that spans multiple elements?
Easy (after a few hours of bumbling around, lol):
- Select the text in the element where you want the block to start (I used
window.find()
) - Save the start point in a range object with
setStart
- Repeat 1 & 2 with the end point, setting the end of the range object
- Select the (multi-node) range object you just created with
window.getSelection().addRange()
Get the code here.
Enjoy!