0

I am trying to create a webpage that will give me the sunrise and sunset times and am using a script I found on GitHub. Below is my code...

<!doctype html>
<!DOCTYPE html>
<html lang="en">
<head>
	<script src="sun.js"></script>
</head>
<body>

<input type="hidden" id="sunrise" value="" />
<input type="hidden" id="sunset" value="" />

<script>
navigator.geolocator.getCurrentPosition(function(position)){
	sunrise = new Date().sunrise(position.coords.latitude, position.coords.longitude)
	sunset = new Date().sunset(position.coords.latitude, position.coords.longitude)
}

document.getElementById('sunrise').value = sunrise;
document.getElementById('sunset').value = sunset;
</script>

//Want to put code here to print out the value of sunrise/sunset

	
</body>
</html>

How would I go about printing the variable value in the HTML body?

2 Answers 2

4

Instead of .value, use .innerHTML.

<!doctype HTML>
<html lang="en">
<head>
	<script src="sun.js"></script>
</head>
<body>

<div id="sunrise"></div>
<div id="sunset"></div>
<script>
navigator.geolocation.getCurrentPosition(function(position) {
	sunrise = new Date().sunrise(position.coords.latitude, position.coords.longitude);
	sunset = new Date().sunset(position.coords.latitude, position.coords.longitude);
})

document.getElementById('sunrise').innerHTML = sunrise;
document.getElementById('sunset').innerHTML = sunset;
</script>

</body>
</html>

I also fixed some syntax errors, and had to replace the <input>s with <div>s so the output could be shown.

Sign up to request clarification or add additional context in comments.

Comments

0

You must remove the 2 input lines since you will generate output:

<input type="hidden" id="sunrise" value="" />
<input type="hidden" id="sunset" value="" />

Then you can use paragraphs instead.

<p id="sunrise"></p>
<p id="sunset"></p>

<script>
// your code..
document.getElementById('sunrise').innerHTML = sunrise;
document.getElementById('sunset').innerHTML = sunset;
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.