Hide Show City and State when zip is populated

lakenney

javascript: I want to show city and state when the zip is filled in. I have the the geo location working but now I just want city and state to show when the zip is filled in.

This is my HTML:

<label for="city">City</label>
<input size="20" placeholder="Town or City" name="city" id="city" type="text">
          <br>
<label for="state">State</label>
<input size="10" placeholder="State/Province" name="state" id="state" type="text">

And this is my JavaScript:

zip.addEventListener("change", getGeo);

function getGeo(e){

// make an send an XmlHttpRequest
var x = new XMLHttpRequest();
x.open("GET","http://maps.googleapis.com/maps/api/geocode/json?address="+this.value,true);
x.send();

// set up a listener for the response
x.onreadystatechange=function(){
  if (this.readyState==4 && this.status==200){

    var c = JSON.parse(this.response).results[0].address_components[1].long_name;
    //alert(c);
    var s = JSON.parse(this.response).results[0].address_components[2].short_name;
      //alert(s);
      if (c) {
          city.value = c;
      }
      if (s) {
          state.value = s;
      }

      //document.getElementById("output").innerHTML = o;
      var l = JSON.parse(this.response).results[0].geometry.location;
      if (l.lat) {
          lat.value = l.lat;
      }
      if (l.lng) {
          lon.value = l.lng;
      }

  }
}
}

It's all in this jsfiddle http://jsfiddle.net/lakenney/gad7ntgk/

Roberto

Looks like you already have a good start, but this example might help a bit. Run the code snippet and enter a postal code or city name. I kept it very simple, but you could check the data array length. When equal to 1 you have a match and could display geo location and other into on the page.

Keep in mind that you're doing a cross domain ajax call ... which will fail in IE < 10. Lots of questions about that on SO if you need help.

<!DOCTYPE HTML>
<html>
<head>
<title>Demo</title>
<style type="text/css">
    #container {position: relative; }
    #location {width: 20em; border: 1px steelblue solid;}
    #results { border: 1px gray solid; padding: 2px; position: absolute;background-color:aliceblue;}
    #results ul { list-style: none; padding: 0; }
    #results ul li {padding: 2px; color: dimgray; }
</style>
</head>
<body>


<div id="container">
    Enter a city or postal code:<br>
    <input id="location" type="text" onKeyup="getLocation()" value="London">
    <div id="results"></div>
</div>

<script type="text/javascript">

function getLocation( ) {
    var value, xhr, html, data, i;
    value = document.getElementById('location').value;
    if (value.length < 2) return;
    xhr = new XMLHttpRequest();
    xhr.open("GET", "http://maps.googleapis.com/maps/api/geocode/json?address=" + value, true );
    xhr.onreadystatechange = function() {
        if (xhr.readyState==4 && xhr.status==200) {
            data = JSON.parse(xhr.responseText);
            html = '';
            for(i=0; i< data.results.length; i++ ) {
                html += '<li>' + data.results[i].formatted_address;
            }
            document.getElementById('results').innerHTML = '<ul>' + html + '</ul>';
        }
    }
    xhr.send();
}
    
getLocation();
document.getElementById('location').focus();

</script>
</body>
</html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Show/hide subview when changing button state

From Dev

Hide Rows when a cell is populated

From Dev

Autofilling state and city based on zip code

From Dev

Autofill city and state from zip code in iOS

From Dev

Autofill City and State with Zip Code entry

From Dev

When creating a user by Membership.CreateUser, is there a .NET wrapper that can set the user's address, city, state, zip, etc.?

From Java

Splitting address with various delimiters into street address, city, state, zip and country

From Dev

Regex for capturing City State Zip from an Address String

From Dev

Parse Dataframe string column into Street, City, State & Zip code

From Dev

Split 'City, State Zip' into three columns in pandas dataframe

From Dev

Bootstrap Inline Form Fields - City, State, Zip inline but with labels and not buttons

From Dev

best data structure for country,state,city,zip-code hierarchy

From Dev

ExtJs - column hide/show state save

From Dev

Checkbox state needs to show or hide a div

From Dev

Laravel - show and hide elements depending on a user state

From Dev

How to hide title "City" when click on that div in php page?

From Dev

Hide and show when click a button

From Dev

Datalist ASP.Net. Only Show Gridlines of cells when populated

From Dev

how to show map of specific city in google map when app start

From Dev

dsmod or other command to edit AD user attributes (Street, P.O. Box, City, State, Zip)

From Dev

Python Doesn't Have Permission To Access On This Server / Return City/State from ZIP

From Dev

Using zippopotam.us to auto-populate zip-code if user enters city and state

From Dev

show when clicked and then hide when clicked again

From Dev

jQuery - show when hidden, hide when showed

From Dev

How do I show or hide a div based on dropdown value populated by PHP query

From Dev

Aggregate by country, state and city

From Dev

State city dropdown list

From Dev

Country City State List

From Dev

get Data from DB using Spring MVC to show in dropdown list like Country, state, city

Related Related

  1. 1

    Show/hide subview when changing button state

  2. 2

    Hide Rows when a cell is populated

  3. 3

    Autofilling state and city based on zip code

  4. 4

    Autofill city and state from zip code in iOS

  5. 5

    Autofill City and State with Zip Code entry

  6. 6

    When creating a user by Membership.CreateUser, is there a .NET wrapper that can set the user's address, city, state, zip, etc.?

  7. 7

    Splitting address with various delimiters into street address, city, state, zip and country

  8. 8

    Regex for capturing City State Zip from an Address String

  9. 9

    Parse Dataframe string column into Street, City, State & Zip code

  10. 10

    Split 'City, State Zip' into three columns in pandas dataframe

  11. 11

    Bootstrap Inline Form Fields - City, State, Zip inline but with labels and not buttons

  12. 12

    best data structure for country,state,city,zip-code hierarchy

  13. 13

    ExtJs - column hide/show state save

  14. 14

    Checkbox state needs to show or hide a div

  15. 15

    Laravel - show and hide elements depending on a user state

  16. 16

    How to hide title "City" when click on that div in php page?

  17. 17

    Hide and show when click a button

  18. 18

    Datalist ASP.Net. Only Show Gridlines of cells when populated

  19. 19

    how to show map of specific city in google map when app start

  20. 20

    dsmod or other command to edit AD user attributes (Street, P.O. Box, City, State, Zip)

  21. 21

    Python Doesn't Have Permission To Access On This Server / Return City/State from ZIP

  22. 22

    Using zippopotam.us to auto-populate zip-code if user enters city and state

  23. 23

    show when clicked and then hide when clicked again

  24. 24

    jQuery - show when hidden, hide when showed

  25. 25

    How do I show or hide a div based on dropdown value populated by PHP query

  26. 26

    Aggregate by country, state and city

  27. 27

    State city dropdown list

  28. 28

    Country City State List

  29. 29

    get Data from DB using Spring MVC to show in dropdown list like Country, state, city

HotTag

Archive