jquery - Send javascript popup results to form field -
i have form captures co-ordinates , send server postcode , returns.
upon clicking location, popup field prefer result go form field instead.
javascript
function showlocation(position) { var latitude = position.coords.latitude; var longitude = position.coords.longitude; $.getjson('http://www.uk-postcodes.com/latlng/' + position.coords.latitude + ',' + position.coords.longitude + '.json?callback=?', null, gotpostcode); } function errorhandler(err) { if(err.code == 1) { alert("error: access denied!"); }else if( err.code == 2) { alert("error: position unavailable!"); } } function gotpostcode(result) { var postcode = result.postcode; $("#postcodegoeshere").val(postcode); } function getlocation(){ if(navigator.geolocation){ // timeout @ 60000 milliseconds (60 seconds) var options = {timeout:60000}; navigator.geolocation.getcurrentposition(showlocation, errorhandler, options); }else{ alert("sorry, browser not support geolocation!"); } }
html
<body> <form> <input type="button" onclick="getlocation();" value="get location"/> </form> <div> <input id='postcodegoeshere' name='xxx' type='text' value='' /> </div> </body>
you have use jquery selector. can put in element using .html(postcode)
or inside input (not checkbox, radio, or button though) using .val(postcode)
function gotpostcode(result) { var postcode = result.postcode; $('#postcodegoeshere').html(postcode); }
information selectors http://api.jquery.com/category/selectors/ , i'm using id selector in code.
changing html in elements http://api.jquery.com/html/
changing input values http://api.jquery.com/val/
Comments
Post a Comment