{{APIRef("DOM")}}{{SeeCompatTable}}
This method is used to retrieve the caret position in a document based on two coordinates. A {{domxref("CaretPosition")}} is returned, containing the found DOM node and the character offset in that node.
Syntax
var caretPosition = document.caretPositionFromPoint(float x, float y);
Parameters
- x
- A horizontal position within the current viewport.
- y
- A vertical position within the current viewport.
Return value
One of the following:
- A {{domxref("CaretPosition")}}.
Null
, if x or y are negative, outside viewport, or there is no text entry node.
Example
This example inserts line breaks wherever you click. The code for it is below the demo.
Demo
{{EmbedLiveSample('Example', '100%', '300px')}}
HTML Content
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
JavaScript Content
function insertBreakAtPoint(e) { var range; var textNode; var offset; if (document.caretPositionFromPoint) { range = document.caretPositionFromPoint(e.clientX, e.clientY); textNode = range.offsetNode; offset = range.offset; } else if (document.caretRangeFromPoint) { range = document.caretRangeFromPoint(e.clientX, e.clientY); textNode = range.startContainer; offset = range.startOffset; } // only split TEXT_NODEs if (textNode.nodeType == 3) { var replacement = textNode.splitText(offset); var br = document.createElement('br'); textNode.parentNode.insertBefore(br, replacement); } } window.onload = function (){ var paragraphs = document.getElementsByTagName("p"); for (i=0 ; i < paragraphs.length; i++) { paragraphs[i].addEventListener("click", insertBreakAtPoint, false); } };
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName("CSSOM View", "#widl-Document-caretPositionFromPoint-CaretPosition-float-x-float-y", "caretPositionFromPoint ")}} |
{{Spec2("CSSOM View")}} | Initial definition |
Browser compatibility
{{CompatibilityTable}}
Feature | Chrome | Firefox | Edge | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | {{CompatNo}}[1] | {{CompatGeckoDesktop("20.0")}}[2] | {{CompatNo}}[1] | {{CompatNo}}[3] | {{CompatNo}}[1] | {{CompatNo}}[1] |
Feature | Android | Firefox Mobile | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | {{CompatNo}} | {{CompatGeckoMobile("20.0")}}[2] | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
[1] WebKit, Blink and MS Edge has implemented document.caretRangeFromPoint(x,y)
from an older W3C draft. Also, note that a {{domxref("Range")}} is returned.
[2] Offsets in {{HTMLElement("textarea")}} and {{HTMLElement("iframe")}} elements aren't correct - see {{bug("824965")}} and {{bug("826069")}} for details.
[3] For MS Internet Explorer the proprietary method TextRange.moveToPoint(x,y)
might be helpful.
See also
- {{domxref("CaretPosition")}}
- {{domxref("Range")}}
- {{domxref("Text")}}
- {{domxref("Selection")}}
- {{domxref("window.getSelection")}}
- {{domxref("Range.getBoundingClientRect()")}} - the inverse for this method (get coordinates of a range)