javascript - How to add an array element to html with jQuery -


i made array 2 elements using browser's built in navigator. gpslocation[0] latitude, , gpslocation1 longitude. before jquery code, using log check gpslocation, confirm both values exist. yet, when assign first element of array html of paragraph element, says undefined. checked coords gave me , they're shockingly accurate. i've searched answer , there's million phrased questions none of them seem trying this.

here code:

function getlocation() {    var gpslocation = [];    if (navigator.geolocation) {      navigator.geolocation.getcurrentposition(function(position) {        gpslocation.push(position.coords.latitude);        gpslocation.push(position.coords.longitude);      });    }    return gpslocation;  }    function getweather() {       var gpslocation = getlocation();    console.log(gpslocation);//looks in console.    $(function(){      $('#test-paragraph').html('latitude: ' + gpslocation[0]);    });  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="weather">    <button id="test-button" onclick="getweather()">test button</button>    <p id="test-paragraph">this sample paragraph learning jquery</p>  </div>

the second function name might seem random weather going purpose. still struggling understand jquery , json.

my console

function getlocation() {      var gpslocation = [];      if (navigator.geolocation) {          navigator.geolocation.getcurrentposition(function(position) {              var pos = {                  latitude: "a",                  longitude: "v"              };              gpslocation.push(pos);          });      }      return gpslocation;  }    function getweather() {      var gpslocation = getlocation();      console.log(gpslocation); //looks in console.      $(function() {          $('#test-paragraph').html('latitude: ' + gpslocation[0]);      });  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="weather">      <button id="test-button" onclick="getweather()">test button</button>      <p id="test-paragraph">this sample paragraph learning jquery</p>  </div>


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -