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
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Search() real simple

    Okay i started a cople of days dont have experience in coding on JavaScript I need to make search button like this ) but i doesn't work. The idea is i add words or something to an array and after i enter the word or something press search and it gives the position in array.

    Code:
    var m = new Array();
    
    
    function add()
    {
          var q = document.getElementById('mEntry');
    	  var temp =  q.value ;
    	  m.push(temp);
         
    }
    function test()
    {
      
    }
    function search()
    {
         var d = document.getEntryById('searchEntry');
         var compare = d.value;
         var i=0;
             for (i=0;i<=m.lenght;i++)
    	 {
                 if( m[i] = compare)
                     document.write(i);
         }
    }

    -------------
    This works just fine )
    Code:
    var m = new Array();
    
    
    function add()
    {
          var q = document.getElementById('mEntry');
    	  var temp =  q.value ;
    	  m.push(temp);
         
    }
    
    
    function test()
    {
      
    }
    
    
    function search()
    {
      
      var sEntry = document.getElementById('searchEntry');
      var key = sEntry.value ;
      for(i in m)
      {
         if(m[i] = key)
    	     {var answer = i;}
      }
      
      document.write(answer);
    
    }
    Last edited by ktelfon; 09-26-2011 at 11:14 AM. Reason: Solved by reading simple stuff )

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,772
    Thanks
    55
    Thanked 518 Times in 515 Posts
    with very little error handling, a simple version would go like this:

    Code:
    <!DOCTYPE html>
    <html>
      <head>
      <title>blank</title>
        <meta charset="UTF-8">
    </head>
    
    <body>
    <input type="text" id="tbox"></input>
    <input type="button" value="Search" onclick="searchWords()">
    <div id="mydiv"></div>
    
    <script type="text/javascript">
    var words =["cat", "dog", "mouse"]
    
    function searchWords() {
    var term=document.getElementById("tbox").value.toLowerCase();
    for (var i = 0; i < words.length; i++) {
    if (term==words[i].toLowerCase()){
    document.getElementById("mydiv").innerHTML="The word "+words[i]+" was found at position "+[i]; 			
    			break;
    			} else {
    document.getElementById("mydiv").innerHTML="The word "+term+" was not found."			
    			}
    		} 
    	} 
    </script>
    </body>
    </html>
    Last edited by xelawho; 09-26-2011 at 03:16 PM.

  • Users who have thanked xelawho for this post:

    ktelfon (09-27-2011)

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Not sure where you want to go with this but here's some code:
    Code:
    <html>
    <head>
    <script>
    Array.prototype.indexesOf = function(toFind,caseInsensitive){
      var foundAt = [], ci = caseInsensitive, x;
      ci && (toFind = toFind.toLowerCase());
      for(var i = 0; i < this.length; i++){
        x = ci ? this[i].toLowerCase() : this[i];
        toFind == x && foundAt.push(i);
      };
      return foundAt;
    };
    
    var findIt = function(form){
      var ar = form.elements[0].value.split(' ');
      var toFind = form.elements[1].value;
      var found = ar.indexesOf(toFind,true);
      document.getElementById('found').innerHTML = found.length ?
        'Found "' + toFind + '" at position ' + found.join(', ') 
        : 'Not found';
    };
    </script>
    </head>
    <body>
    <form onsubmit="findIt(this);return false">
    My text:<br/>
    <textarea style="width:400px;height:200px"></textarea>
    <br/><br/>
    Find:<br/>
    <input/><input type="submit" value="Find"/>
    </form>
    <div id="found"></div>
    </body>
    </html>
    Last edited by ironboy; 09-26-2011 at 08:47 PM.

  • Users who have thanked ironboy for this post:

    ktelfon (09-27-2011)


  •  

    Posting Permissions

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