Saturday, February 5, 2011

How can the selection of a textarea be determined/manipulated in Safari from JavaScript?

I have some JavaScript code that does two things:

  • retrieves the selected text from a text area
  • adds a prefix and suffix to that selected text

The code currently uses the selectionStart and selectionEnd properties of the textarea to figure out where the selection starts/end, extracts the selection text and processes it, and then re-writes the value of the textarea with the prefix and suffix inserted in the right spots.

This implementation works fine in Firefox but it does not work in Safari. Apparently the selectionStart and selectionEnd properties do not exist in WebKit. (I suspect that the code also does not work in Google Chrome, which is also WebKit-based, though I have not tested this.)

How can I perform these two operations in Safari?

0 comments:

Post a Comment