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.

Syntax:

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

Parameters:

start
Required. Integer that specifies the zero-based start index.
length
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:
<head>
    <script type="text/javascript">
        function GetLastTwo () {
            var container = document.getElementById ("container");
            var textNode = container.firstChild;


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

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content