sasperilla wrote:
If the user highlights or selects text in a div or span can I get the
indicies that make up that selected piece of text? What API of
javascript do I use to find that out? Or really is there a way in
javascript to interact with the browsers text selection?
Browser specific, IE/Win has
document.selection
has an object with a method createRange
var range = document.selection.createRange()
where that range has lots of properties and methods to inspect and
manipulate it:
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/obj_selection.asp>
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/obj_textrange.asp>
Mozilla gives you its (very different) selection object with
window.getSelection()
and that can be manipulated, partly with proprietary Mozilla stuff,
partly based on the W3C DOM Level 2 Range API (selection consists of one
or more such ranges)
<http://xulplanet.com/references/objref/Selection.html>
<http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html>
Opera 9 preview also supports
window.getSelection()
and seems to follow the Mozilla implementation though I am not sure how
complete that is currently and how complete it is intended to become.
But if text is selected somewhere in the HTML document then e.g.
window.getSelection().getRangeAt(0).deleteContents ()
does remove the selected contents.
As for selection specific events, IE has lots of them, see
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/events/onselectstart.asp>
for instance.
Mozilla gives you two properties on the event object
rangeParent
rangeOffset
that relate to the range(s) used with the selection object.
--
Martin Honnen
http://JavaScript.FAQTs.com/