Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts

    JavaScript Searching

    Are there any methods for searching on the client side w/ javascript? Even if its just searching a variable for a certain string. Are there any methods of doing this?

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    495
    Thanks
    0
    Thanked 82 Times in 80 Posts
    This will find and highlight text in the document:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>highlight</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <script type="text/javascript">
    function find(searchTerm){
    var contents=document.body.innerHTML;
    var idx=contents.search(searchTerm);
    if(idx) {
        contents=contents.replace(searchTerm,'<span id="highlight">'+searchTerm+'</span>');
        document.body.innerHTML=contents;
        document.getElementById('highlight').scrollIntoView();
        }
    }
    </script>
    
    <style type="text/css">
    #highlight {background:yellow;}
    </style>
    
    </head>
    <body>
    <label>Find on this page: <input type="text" id="search"></label><button type="button" onclick="find(document.getElementById('search').value);">Highlight it</button>
    <h1>Nirafo</h1>
    <p>Aphasm escit ra apovum ecripsat. Quorepl alist osmephum ime itasmotis to eleh. Quadeth est. Coticreus oce ro omuf ari trolodeus est. Nosm est. Thihopsiem, minus beh brofiprue teranis. </p>
    <h2>Smopada</h2>
    <h2 onclick="alert('hi');">Edrecram Ol Quorobucranis</h2>
    <h3>Funap</h3>
    <p>Ohusch ifemes, presn mapliant ple pupriem nalaho. Acrutra son cro teton hi prenaquid est. Ocophiant asi pso bratetriem eta cragasch est. Volespes in aplasnem usnerhant. Gerociant ina quidu tefum ag hiphue est. </p>
    <p>Trog crin maquor on crebatuer tes efascranis. Ehisc pibisnam snatofas, ic smot pel vavetont. Drup nicrus quido edesmiem plarodonis. Ugepsiant sosch log fathon atahotis. Stim dabr quorimapseus vi dathat ufo lequadat. Quoreh muspeus quidicusp, bro psopl tutist ropinuer. Schops tremorh, otusm lusagore dro aquorospore pluvusmont. Socelum in rhetapa tumotis. Tof, vufucrue ul uquadicrum ulovant. Smotr ruquidi ipreprant, an ugist popupr mucanis. Acron osmac ipeh vav iloquader quadi govedas. Das mipacras, scet roteprore tin tabesch opod. Escreh quadot dipapam nan hubo bre etomas. Sorhiant, vasta quoro opraquoriant meriplotis. </p>
    <p>Apoquid equoraphes mo buthi apretrotis. Fasm ohesmus, phud planerhes pe mafothius itohonis. Brigiquorum in imischius vicrat. Smubr est. Orhan ipsev tigigue ana omosmer ide tacront. </p>
    </body>
    </html>

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You can use regular expressions to search for a sub-string within a string:-

    E.g:-

    var str = "The quick brown fox jumps over the lazy dog";
    x = str.match (/(fox)/gi);
    alert (x);

    You can test your regular expressions at: http://www.ogauge.co.uk/regextester.html


    Fang: something is wrong with your version of this script. A working version can be found at:-
    Tough JS question: Parsing Search Terms
    Last edited by Philip M; 11-20-2007 at 09:27 AM. Reason: Tried Fang's script - does not work!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •