By sjoerd2002
How can I link my Javascript function in an external file to my html file? Here you can see my code from my html and Javascript. I tried with the <script> but this didn’t work out :(
**HTML**
<html>
<head>
<title>Digital Clock</title>
<link rel="stylesheet" href="clock.css">
</head>
<body>
<h1>Digital Clock</h1>
<script type="text/javascript" src="clock.js"></script>
</script>
</body>
</html>
**Javascript**
function showTime(){
var date = new Date();
var h = date.getHours(); // 0 - 23
var m = date.getMinutes(); // 0 - 59
var s = date.getSeconds(); // 0 - 59
var session = "AM";
if(h == 0){
h = 12;
}
if(h > 12){
h = h - 12;
session = "PM";
}
h = (h < 10) ? "0" + h : h;
m = (m < 10) ? "0" + m : m;
s = (s < 10) ? "0" + s : s;
var time = h + ":" + m + ":" + s + " " + session;
document.getElementById("MyClockDisplay").innerText = time;
document.getElementById("MyClockDisplay").textContent = time;
setTimeout(showTime, 1000);
}
showTime();
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi @sjoerd2002
I can see in your function that you are trying to get an HTML element by id which should be MyClockDisplay however I can’t see that element ID anywhere in your HTML code.
Basically, you want to create a div where the ID would be MyClockDisplay and it would display the time.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.