I am making a music player (just to play one song so i can learn more html) and i want it to display the time at the bottom and it seems the only way to do it is with Javascript. I got it to print the time, but it doesnt display any of the html code, only the time shows.
JavaScript
x
10
10
1
function update() {
2
var today = new Date();
3
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
4
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
5
var dateTime = date + ' ' + time;
6
document.body.innerHTML = dateTime
7
}
8
window.onload = function() {
9
update();
10
setInterval(update,
JavaScript
1
18
18
1
body {
2
margin: 0;
3
display: flex;
4
justify-content: center;
5
align-items: center;
6
flex-direction: column;
7
height: 100vh;
8
}
9
10
h1,
11
h2 {
12
background: -webkit-linear-gradient(purple, pink);
13
-webkit-background-clip: text;
14
-webkit-text-fill-color: transparent;
15
font-size: 30px;
16
text-align: center;
17
align-content: center;
18
}
JavaScript
1
19
19
1
<h2>Hall Of Fame</h2>
2
<img src=https://dl.dropbox.com/s/uw9v5ro9a6650bd/88DFC197-4C1C-45C4-B8A9-85344796CC74.jpeg? height=150px width=1 50px></img>
3
4
<body>
5
<h1>Let's listen to some music</h1>
6
<div class="audio">
7
8
<audio controls>
9
<source src="https://dl.dropboxusercontent.com/s/trelm7752nmamcm/Ireland%20Boys%20x%20NCK%20-%20Hall%20of%20Fame%20%28Official%20Music%20Video%29.mp3"
10
11
type="audio/mpeg"></source>
12
13
<source src="https://drive.google.com/file/d/0ByyrPyKgcWhBZ3ViZ19QYllxYjA/view?usp=drivesdk"
14
15
type="audio/mpeg"></source>
16
</audio>
17
18
</div>
19
<marquee scrollamount=2 direction=right>Ireland boys X NCK - Hall Of Fame</marquee>
Advertisement
Answer
you are adding time to entire body, you should try this
JavaScript
1
18
18
1
<script>
2
function update() {
3
var timeWrap = document.createElement("div");
4
timeWrap.setAttribute("id","timeWrap");
5
var timeWrapEle = document.getElementById("timeWrap");
6
if(timeWrapEle === null){
7
document.body.appendChild(timeWrap);
8
var timeWrapEle = document.getElementById("timeWrap");
9
}
10
var today = new Date();
11
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
12
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
13
var dateTime = date+' '+time;
14
timeWrapEle.innerHTML = dateTime
15
}
16
window.onload = function() {update(); setInterval(update, 1000);}
17
</script>
18