I have an array containing city names and postal codes that I need to search through. I can successfully search for the city name and retrieve the result, but when I try to search for a postal code, it doesn't register. My question is: How can I modify my search function to accommodate this?
Here's a snippet of my script and array:
Sample of the array:
Array
(
[0] => Array
(
[0] => 9900
[1] => Town 1
)
[1] => Array
(
[0] => 9900
[1] => Town 2
)
[2] => Array
(
[0] => 9940
[1] => Town 3
)
[3] => Array
(
[0] => 9970
[1] => Town 4
)
[4] => Array
(
[0] => 9981
[1] => Town 5
)
)
$(document).ready(function(){
$(".searchinput").keyup(function(){
var b = document.getElementsByClassName('searchinput')[0].value;
var b = document.getElementById("Searchfield")
var SearchValue=b.value;
var i=0, k=0, indx=[], msg;
for ( i=0; i<postnummer.length; i++)
{ for ( k=0; k<postnummer[i].length; k++)
{ if (postnummer[i][k] === SearchValue){ indx = [i,k]; break; }
} }
if(typeof indx[0] == "undefined" || typeof indx[1] == "undefined"){
msg=("Not found"); }
else { msg="i= "+indx[0]+" k= "+indx[1]; }
var a = document.getElementById("Result");
a.value = b.value + " - " + msg ;
});
});
When I type in "Town 2," I get the expected result: "Town 2 - i= 1 k= 1."
However, I'm unable to retrieve the listing with the "0" index in the array (the postal code 9900) in the result, nor can I search for the number 9900 in the array to return a valid result.