You are here: Reference > JavaScript > client-side > selection and ranges > methods > findText (TextRange)
findText method (TextRange)
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:
You can find the related objects in the Supported by objects section below.
Parameters:
Required. String that specifies the text to find. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Optional. Integer that specifies the direction of the search.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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:
|
Return value:
Boolean. One of the following values:
No match was found. | |
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?
|
Supported by objects:
Related pages:
External links:
User Contributed Comments