You are here: Reference > JavaScript > client-side > selection and ranges > methods > findText (TextRange)

findText method (TextRange)

Browser support:
Searches for a specified text in the document, relative to a TextRange object.
If a match is found, then it modifies the start and end points of the TextRange object to correspond to the matching text.
Use the find method for finding texts in Firefox, Google Chrome and Safari.

Syntax:

object.findText (textToFind [, direction [, flags]]);
You can find the related objects in the Supported by objects section below.

Parameters:

textToFind
Required. String that specifies the text to find.
direction
Optional. Integer that specifies the direction of the search.
  • Negative values mean that the target interval of the search is from the beginning of the document to the end point of the TextRange object. The direction of search is always reversed, regardless of the value of the flags parameter.
  • A value of zero means that the start and end points of the TextRange object specify the target interval of the search. It always indicates forward search, regardless of the value of the flags parameter.
  • When a positive value is specified and the value of the flags parameter does not contain the backwards flag (even), then the target interval of the search is from the start position of the TextRange object to the end position of the document and it means forward search.
    If the value of the flags parameter contains the backwards flag (odd), then the target interval of the search is from the start position of the document to the end position of the TextRange object and it means backward search.
    The default is a positive value for this parameter.
flags
Optional. Integer that indicates the type of search. The default value is 0.
The value can be any combination of the following integer constants with the bitwise OR operator:
1
The backwards flag. See the description of the direction parameter.
2
The whole words flag. If specified, only whole words will be matched, else partial match is allowed.
4
The case-sensitive flag. If specified, only case-sensitive match is allowed.
0x00020000
Match bytes.
0x20000000
Match diacritical characters.
0x40000000
Match Kashida characters.
0x80000000
Match AlefHamza characters.

Return value:

Boolean. One of the following values:
false No match was found.
true A match was found.

Example HTML code 1:

This example illustrates a cross-browser solution for finding text within a page:
<head>
    <script type="text/javascript">
        function FindNext () {
            var str = document.getElementById ("findField").value;
            if (str == "") {
                alert ("Please enter some text to search!");
                return;
            }

            var supported = false;
            var found = false;
            if (window.find) {        // Firefox, Google Chrome, Safari
                supported = true;
                    // if some content is selected, the start position of the search 
                    // will be the end position of the selection
                found = window.find (str);
            }
            else {
                if (document.selection && document.selection.createRange) { // Internet Explorer, Opera before version 10.5
                    var textRange = document.selection.createRange ();
                    if (textRange.findText) {   // Internet Explorer
                        supported = true;
                            // if some content is selected, the start position of the search 
                            // will be the position after the start position of the selection
                        if (textRange.text.length > 0) {
                            textRange.collapse (true);
                            textRange.move ("character", 1);
                        }

                        found = textRange.findText (str);
                        if (found) {
                            textRange.select ();
                        }
                    }
                }
            }

            if (supported) {
                if (!found) {
                    alert ("The following text was not found:\n" + str);
                }
            }
            else {
                alert ("Your browser does not support this example!");
            }
        }
    </script>
</head>
<body>
    <div>LaLa, Lala, laLa , lala, lalala, tralala, some other text</div>
    <br />
    <input type="text" id="findField" value="lala" size="20" />
    <button onclick="FindNext ();">Find!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content