You are here: Reference > JavaScript > client-side > HTML DOM > methods > substringData (comment, CommentNode, TextNode)

substringData method (comment, CommentNode, TextNode)

Browser support:
Returns the part of the current element's text content from the specified position with the specified length.
This method has the same effect as the substr method on the data of the current element.


object.substringData (start, length);
You can find the related objects in the Supported by objects section below.


Required. Integer that specifies the zero-based start index.
Required. Integer that specifies the maximum number of returned characters.

Return value:

String that contains the specified segment of text.

Example HTML code 1:

This example illustrates the use of the substringData method:
    <script type="text/javascript">
        function GetLastTwo () {
            var container = document.getElementById ("container");
            var textNode = container.firstChild;

            var lastTwo = textNode.substringData ( - 2, 2);
                // or:
                // var lastTwo = ( - 2, 2);
            alert (lastTwo);
    <div id="container">12345678</div>
    <br />
    <button onclick="GetLastTwo ()">Get the last two characters</button>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content