Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-26-2005, 07:41 AM   PM User | #1
Vapor
Regular Coder

 
Join Date: May 2005
Posts: 220
Thanks: 1
Thanked 2 Times in 2 Posts
Vapor is an unknown quantity at this point
text search not case sensitive

Basscyst if you read this thread (or anyone who can fix this)

I was given the following code:

Code:
<!DOCTYPE html

PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

            <head>

                        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

                        <title></title>

                        <style type="text/css">

                                    .highlight

                                    {

                                                background-color:yellow;

                                    }

                        </style>

                        <script type="text/javascript">

                                    function searchText()

                                    {

                                                var obj1=document.getElementById('srch_str');

                                                var obj2=document.getElementById('txt');

                                                

                                                var str1=obj1.value;

                                                var str2=obj2.value;

                                                

                                                var cut=str2.split(str1);

                                                var len=cut.length;

                                                var new_str="";

                                                for(var i=0;i<len;i++)

                                                {

                                                            if(i!=len-1)

                                                            {

                                                                        new_str+=cut[i]+'<span class="highlight">'+str1+'</span>';

                                                            }

                                                            else

                                                            {

                                                                        new_str+=cut[i];

                                                            }

                                                }

                                                if(len==1)

                                                {

                                                            new_str="No Match Found!";

                                                }

                                                document.getElementById('results').innerHTML=new_str;

                                    }

                        </script>

            </head>

            <body>

                        <form>

                                    Search: <input type="text" id="srch_str" /> 

                                    <input type="button" onclick="searchText()" />

                                    <br />

                                    <textarea cols="40" rows="5" id="txt">Hello all.  I am testing a search function. It is because of this that this text is here.

                                    </textarea>

                        </form>

                        Results:

                        <div id="results">

                        </div>   

            </body>

</html>
This is a code to be able to search and highlight text within the textarea box. The only problem I run into is the fact that it is case sensitive. How can I search for TEST and still have it highligh test?
Vapor is offline   Reply With Quote
Old 05-26-2005, 07:55 AM   PM User | #2
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
You could change the search string and text to all upper case or lower case:
Code:
var str1=obj1.value.toLowerCase();
var str2=obj2.value.toLowerCase();
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 05-26-2005, 08:01 AM   PM User | #3
Vapor
Regular Coder

 
Join Date: May 2005
Posts: 220
Thanks: 1
Thanked 2 Times in 2 Posts
Vapor is an unknown quantity at this point
While I am here, I have another question.

How can I make is so that once I type something into the search line I can just hit enter and it will search rather then having to click on the button?
Vapor is offline   Reply With Quote
Old 05-26-2005, 08:03 AM   PM User | #4
Harry Armadillo
Regular Coder

 
Join Date: Feb 2005
Posts: 400
Thanks: 0
Thanked 0 Times in 0 Posts
Harry Armadillo is on a distinguished road
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title></title>
    <style type="text/css">
      .highlight {
        background-color:yellow;
        }
    </style>
    <script type="text/javascript">
      function searchText(){
        var str=document.getElementById('srch_str').value;
        var old_str=document.getElementById('txt').value;
        var regex=new RegExp('('+str+')','ig');
        
        var new_str=old_str.replace(regex,'<span class="highlight">$1</span>') ;
        if(new_str.length==old_str.length){
          new_str="No Match Found!";
          }
        document.getElementById('results').innerHTML=new_str;
        }
    </script>
  </head>
  <body>
    <form onsubmit='searchText();return false'>
      Search: <input type="text" id="srch_str" /> 
      <input type="submit" value="Find Text"/>
      <br />
      <textarea cols="40" rows="5" id="txt">Hello all.  I am testing a search function. It is because of this that this text is here.
      </textarea>
    </form>
    Results:
    <div id="results">
    </div>   
  </body>
</html>

Last edited by Harry Armadillo; 05-26-2005 at 08:37 AM..
Harry Armadillo is offline   Reply With Quote
Old 05-26-2005, 08:07 AM   PM User | #5
Vapor
Regular Coder

 
Join Date: May 2005
Posts: 220
Thanks: 1
Thanked 2 Times in 2 Posts
Vapor is an unknown quantity at this point
Brandoe85,

I don't quite get where to stick that bit of code. You see I want to make it so that if someone types Test instead of test it will still find the word test even if the first letter is capitalized, not nessacerly all caps
Vapor is offline   Reply With Quote
Old 05-26-2005, 08:10 AM   PM User | #6
Vapor
Regular Coder

 
Join Date: May 2005
Posts: 220
Thanks: 1
Thanked 2 Times in 2 Posts
Vapor is an unknown quantity at this point
thanks, that last code did work.

Thank you!
Vapor is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:51 PM.


Advertisement
Log in to turn off these ads.