Hello and welcome to our community! Is this your first visit?
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
    AZ, USA
    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. #2
    Regular Coder
    Join Date
    Jun 2004
    Thanked 82 Times in 80 Posts
    This will find and highlight text in the document:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <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>');
    <style type="text/css">
    #highlight {background:yellow;}
    <label>Find on this page: <input type="text" id="search"></label><button type="button" onclick="find(document.getElementById('search').value);">Highlight it</button>
    <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 onclick="alert('hi');">Edrecram Ol Quorobucranis</h2>
    <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>

  3. #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,703 Times in 2,679 Posts
    You can use regular expressions to search for a sub-string within a string:-


    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:-
    Last edited by Philip M; 11-20-2007 at 10: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