Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

303
Views
Why did I get [object HTMLParagraphElement] for value of variable(geolocation)

Here is the code which will get users coordinate and save the coordinates in local storage but it displaying [object HTML Paragraph Element] as stored value if i can save it as numeric value it would be easy to export i am a newbee please help me to solve this problem

var x = document.getElementById("demo");
var k = document.getElementById("demon");

localStorage.setItem("cor",k);

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
      } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";

  }
}

function showPosition(position) {
  x.innerHTML = "Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;
 k.innerHTML =position.coords.latitude
   
}
<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>
<p id="demon"></p>

</body>
</html>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

LocalStorage only stores items as string values. So

localStorage.setItem("cor",k);

converts k (an HTMLParagraphElement) to a string, " [object HTML Paragraph Element]" in your browser, and saves it as item "cor".

To save the coordinates, try converting them to a JSON string, after obtaining them in the showPosition callback, by calling JSON.stringify with a coordinate object as argument:

function showPosition(position) {
  const {latitude, longitude} = position.coords;
  // save in local storage:
  localStorage.setItem("cor",JSON.stringify({latitude, longitude});
  // as posted:
  x.innerHTML = "Latitude: " + latitude + 
  "<br>Longitude: " + longitude;
  k.innerHTML =position.coords.latitude
   
}

To read the coordinates back again, pass the string value of getItem("cor") to JSON.parse to re-create the {latitude, longitude} object.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!