javascript - div doesn't update on Calling watchLocation -


below html5 code finding updated geolocation. latitude , longitude values pops in alert box. but, value cannot assigned div using $(id).html(value); . using watchposition() function.

   <html>      <head>          <script type="text/javascript">          var watchid;          var geoloc;           function showlocation(position) {             var latitude = position.coords.latitude;             var longitude = position.coords.longitude;             alert("latitude : " + latitude + " longitude: " + longitude);              $("#lat").html(latitude);              $("#long").html(longitude);              }                    function errorhandler(err) {             if(err.code == 1) {                alert("error: access denied!");             }              else if( err.code == 2) {                alert("error: position unavailable!");             }          }           function getlocationupdate(){             if(navigator.geolocation){                // timeout @ 60000 milliseconds (60 seconds)                var options = {timeout:60000};                geoloc = navigator.geolocation;                watchid = geoloc.watchposition(showlocation, errorhandler, options);             }              else{                alert("sorry, browser not support geolocation!");             }          }       </script>     </head>    <body>        <form>          <input type="button" onclick="getlocationupdate();" value="watch update"/>          latitude : <div id="lat"></div>          longitude : <div id="long"></div>        </form>      </body> </html> 

when use alert() stops further execution of code, move alert() below lines assigning value div , should solve problem.

$("#lat").html(latitude); $("#long").html(longitude); alert("latitude : " + latitude + " longitude: " + longitude); 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -