anchorOffset
Summary
Returns the number of characters that the selection’s anchor is offset within the anchorNode.
Property of dom/Selectiondom/Selection
Syntax
Note: This property is read-only.
var offset = selObj.anchorOffset;
Return Value
Returns an object of type NumberNumber
Examples
The following example shows the anchorOffset value of a selection when you release the mouse button.
<!DOCTYPE html>
<html>
<head>
<!-- this example shows the character offset from anchor node of your selection-->
<title>AnchorOffset Example</title>
<script type="text/javascript">
function getAnchorOffset() {
if (window.getSelection) { //only work if supported
var selection = window.getSelection (); //get the selection object
var anchorOffsetProp = selection.anchorOffset; //get the offset
alert ( "Anchor Offset: \n" + anchorOffsetProp.toString());
}
}
</script>
</head>
<body>
<div onmouseup="getAnchorOffset()"> <!-- call this function when the mouse button is released -->
<p>
Select some text with your mouse within this field.
When <strong>the left <em>button</em> is released</strong>, a dialog pops up with the anchor offset.
</p>
<p>
The nested tags <strong>here and <em>there</em> can</strong> demonstrate different offsets as well.
</p>
</div>
</body>
</html>
Notes
Remarks
The value that is returned by anchorOffset usually refers to a character position within the text portion of the element.
Syntax
offset=selObj.anchorOffset;
Standards information
- HTML5 A vocabulary and associated APIs for HTML and XHTML, Section 7.6.1
Attributions
Mozilla Developer Network : [Selection.anchorOffset Article]
Microsoft Developer Network: [anchorOffset Property Article]