Archive

Posts Tagged ‘JavaScript’

Change selection in Safari via JavaScript

November 19th, 2009 Sean DeNigris No comments

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):

  1. Select the text in the element where you want the block to start (I used window.find())
  2. Save the start point in a range object with setStart
  3. Repeat 1 & 2 with the end point, setting the end of the range object
  4. Select the (multi-node) range object you just created with window.getSelection().addRange()

Get the code here.

Enjoy!

Categories: Mac Tags: ,