...

View Full Version : Search() real simple



ktelfon
09-26-2011, 10:08 AM
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.



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 )


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);

}

xelawho
09-26-2011, 02:45 PM
with very little error handling, a simple version would go like this:



<!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>

ironboy
09-26-2011, 08:38 PM
Not sure where you want to go with this but here's some 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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum