Reputation: 21877
I have the following piece of JS code within a function that responds to the user pressing enter (not displayed). The part of the code I am concerned with is shown:
$.each(cityhash,function(key,value){
if(value['city']== user_input) {
$('#city').empty().append(value['city']);
$('#state').empty().append(value['city']);
}
I have the following hash:
cityhash = [{"address":"07288 Albertha Station","city":"Littelside","state":"Missouri"},{"address":"0615 Mervin Rapid","city":"Tessmouth","state":"South Carolina"},{"address":"779 Elody Lock","city":"Littelside","state":"New Mexico"}]
As you can see, the city of Littelside appears twice in the hash. My problem is the above $.each function, only displays 1 of the 2 Littlesides. I would like to show all matches in the hash, not just 1 match.
How can I correct my code to return all matching cities rather than just showing one city?
Thank you in advance
Upvotes: 0
Views: 1310
Reputation: 7179
You need to get rid of the empty() calls. You are clearing out #city/#state on each match.
$('#city').append(value['city']);
$('#state').append(value['state']);
Also, you probably want to append the state name and not the city name to the #state element
Upvotes: 3