moveToPoint
Summary
Moves the start and end positions of the text range to the given point.
Method of dom/TextRangedom/TextRange
Syntax
var result = textRange.moveToPoint(/* see parameter list */);
Parameters
x
- Data-type
- Number
Integer that specifies the horizontal offset relative to the upper-left corner of the window, in pixels.
y
- Data-type
- Number
Integer that specifies the vertical offset relative to the upper-left corner of the window, in pixels.
Return Value
Returns an object of type NumberNumber
Type: HRESULT
If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
Examples
This example uses the moveToPoint method to move the text range to the point where the user clicked the mouse, expands the range, and selects the text within the new range.
<SCRIPT FOR=document EVENT=onclick LANGUAGE="JScript">
var rng = document.body.createTextRange();
rng.moveToPoint(window.event.x, window.event.y);
rng.expand("word");
rng.select();
</SCRIPT>
The following example moves the caret in harmony with the mouse as it moves over a contenteditable div element.
<!DOCTYPE html>
<html>
<head>
<title>moveToPoint example</title>
</head>
<body>
<div contenteditable="true" onmousemove="followMouse()">
Move the mouse over this text, the caret will follow it.
</div>
<script type="text/javascript">
function followMouse() {
if (document.body.createTextRange) {
var range = document.body.createTextRange();
range.moveToPoint(event.clientX, event.clientY);
range.select();
}
}
</script>
</body>
</html>
Notes
Remarks
The coordinates of the point must be in pixels and be relative to the upper-left corner of the window. The resulting text range is empty, but you can expand and move the range using methods such as expand and moveEnd. This feature might not be available on non-Microsoft Win32 platforms.
Attributions
Microsoft Developer Network: [moveToPoint Method Article]