Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to highlight the text which is entered as an input.Am using window.find() in javascript.If the content is outside of the textarea window.find() works fine.But if the content is inside of the textarea it seems like not working. just first occurence of string only getting hightlighted,am not gettin all of the occurences of string hightlighted inside textarea.

Here is my code.

XML
<pre lang="HTML"><pre lang="HTML"></pre><head>
    <script type="text/javascript">
        function FindNext () {
            var str = document.getElementById ("findInput").value;
            if (str == "") {
                alert ("Please enter some text to search!");
                return;
            }

            if (window.find) {        // Firefox, Google Chrome, Safari
                var found = window.find (str);
                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="findInput" value="lala" size="20" />
    <button onclick="FindNext ();">Find Next</button>
</body>
</pre>




Here the code is for only div..I need this working inside textarea also and I want to all the occurences of found string highlighted.
Thanks for reading
Posted

1 solution

First of all, it's not very useful to use the API which is not available in all browsers. See also the Mozilla warning (bug 672395):
https://developer.mozilla.org/en-US/docs/Web/API/Window.find[^],
https://bugzilla.mozilla.org/show_bug.cgi?id=672395[^].

However, if you wish to ignore the poor IE, it might work; too bad it's not a standard part of Javascript-in-a-Web-browser environment.

Now, it won't work with data entered in input controls, so what? You will need to find the strings in those values separately, using the property value and string search methods:
http://www.w3schools.com/jsref/jsref_search.asp[^],
http://www.w3schools.com/jsref/jsref_indexof.asp[^].

You can also use Regular Expression search: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search[^].

—SA
 
Share this answer
 
v2
Comments
Vidhya Raju 12-Aug-14 2:52am    
can you pls tell me how to achieve this in php?
Sergey Alexandrovich Kryukov 12-Aug-14 3:01am    
Why PHP? This is server-side. Achieve what? Completely different question. Look PHP documentation of strpos and regex, ask a separate question if there are any concerns...
—SA
Vidhya Raju 12-Aug-14 2:58am    
just I need,if user enters an input in the text,all the occurences of found string should be hightlighted inside textarea
Sergey Alexandrovich Kryukov 12-Aug-14 3:02am    
Selection is also a separate question; not sure it's possible. I answered your original question, are you going to accept it formally (green "Accept" button)?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900