var d = new Date();
var day;
switch (d.getDay()) {
case 0 :
day = "Sunday";
break;
case 1 :
day = "Monday";
break;
case 2 :
day = "Tuesday";
break;
case 3 :
day = "Wednesday";
break;
case 4 :
day = "Thursday";
break;
case 5 :
day = "Friday";
break;
default :
day = "Saturday";
}
document.getElementById("demo").innerHTML = "Today is : " ;
Asked
Active
Viewed 82 times
-5
-
7What error is it giving? – Carcigenicate Jul 09 '18 at 13:12
-
This code seems to work for me - I get an "Monday" in the `day` variable. – Lix Jul 09 '18 at 13:14
-
1Provided that `#demo` exists, `document.getElementById("demo").innerHTML = "Today is : " + day;` – Gerardo Furtado Jul 09 '18 at 13:14
-
2I'm guessing the issue is `document.getElementById("demo")` returns undefined. – Jul 09 '18 at 13:14
-
Uncaught TypeError: Cannot set property 'innerHTML' of null at myscript.js:27 #copied from console i.e. the last line – Vedant Jul 09 '18 at 13:14
-
See the comment above from @Amy. That element doesn't exist when you try to set the innerHTML property. – Reinstate Monica Cellio Jul 09 '18 at 13:15
-
3@Archer - Your edit actually removes the "issue" that the OP is talking about (missing HTML element) - I've rolled back your edit FYI. – Lix Jul 09 '18 at 13:16
-
Include your HTML. – G Cadogan Jul 09 '18 at 13:16
-
1@Lix - fair play. At the time it didn't and I didn't want to introduce an error so I added the element :p – Reinstate Monica Cellio Jul 09 '18 at 13:17
-
I have used the demo id in the html file and saved this javascript file seperately – Vedant Jul 09 '18 at 13:17
-
1But *where* in the HTML are you referencing the js file? Before or after the element with that ID? – Gerardo Furtado Jul 09 '18 at 13:18
-
Probably a case of needing to wait for the DOM to load before you modify it. Put the script tags at the bottom of the page - last thing inside the body tag. – Reinstate Monica Cellio Jul 09 '18 at 13:18
-
@ Gerardo Furtado after the element with the id – Vedant Jul 09 '18 at 13:27
1 Answers
-1
Add HTML and missing day
and append at the end before assignment
var d = new Date();
var day;
switch (d.getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
default:
day = "Saturday";
}
document.getElementById("demo").innerHTML = "Today is : " + day;
<div id="demo"></div>

Rikin
- 5,351
- 2
- 15
- 22
-
downvote for a valid solution without mention of what's wrong and what could be the improvement to the answer, not helping anyone. – Rikin Jul 09 '18 at 17:22