PDA

View Full Version : Resolved if no matches, show alert



xelawho
01-21-2011, 08:57 PM
hi,

I've been making a search box, which works as it should like this:


function searchLocations() {
var input = document.getElementById('tb').value;

if (!input) {
alert("please enter a name");
} else {
for (var j = 0; j < gmarkers.length; j++) {

if (gmarkers[j].myname.toLowerCase() == input.toLowerCase()) {
gmarkers[j].show();
myclick(j);
}
}
}
}

but then if the user types a term that doesn't match, it does nothing. So I wanted an alert, which I figured would go like this:


function searchLocations() {
var input = document.getElementById('tb').value;
for (var j = 0; j < gmarkers.length; j++) {
if (input.toLowerCase() == gmarkers[j].myname.toLowerCase()) {
gmarkers[j].show();
myclick(j);
} else
alert("no results found");
}
}

or this:


function searchLocations() {
var input = document.getElementById('tb').value;
for (var j = 0; j < gmarkers.length; j++) {
if (gmarkers[j].myname.toLowerCase() !== input.toLowerCase()) {
alert("no results found");
} else {
if (gmarkers[j].myname.toLowerCase() == input.toLowerCase()) {
gmarkers[j].show();
myclick(j);
}
}
}
}


but either way pops an alert, even if the search term is valid, and locks the page up.

which is not quite what I was looking for.

any suggestions?

Old Pedant
01-21-2011, 09:09 PM
function searchLocations()
{
var found = false;
var input = document.getElementById('tb').value;
for (var j = 0; j < gmarkers.length; j++)
{
if (gmarkers[j].myname.toLowerCase() == input.toLowerCase())
{
found = true;
gmarkers[j].show();
myclick(j);
}
}
if ( ! found ) alert("Not found");
}


}

xelawho
01-21-2011, 09:22 PM
:thumbsup: thanks, Old Pedant