I am working on an HTML file that has a drop-down list of months. When I select a month, I want the function to show the dates of the month on the page in this format:
1st of December 2020
2nd of December 2020
.
.
.
31st of December 2020
Below is my html:
I cannot get the function to execute on option select. Could you help me what is wrong with the script? I am no expert in javascript yet but I am continuously exploring and experimenting with HTML and javascript.
Please note that I will be add the codes for the other months when I get the function to execute for these two months.
JavaScript
x
45
45
1
var ord = "";
2
var i;
3
var fulldate;
4
var y = "";
5
var m = document.getElementById("month");
6
var mv = m.value;
7
8
function showdates() {
9
if (mv == "December") {
10
fulldate = "";
11
y = "2020";
12
for (i = 1; i < 32; i++) {
13
if (i > 3 && i < 21) {
14
ord = "th";
15
} else if (i == 1 || i == 21 || i == 31) {
16
ord = "st";
17
} else if (i == 2 || i == 22) {
18
ord = "nd";
19
} else if (i == 3 || i == 23) {
20
ord = "rd";
21
} else {
22
ord = "th";
23
}
24
fulldate += i + ord + " of " + mv + "" + y + "<br>";
25
}
26
} else if (mv == "January") {
27
fulldate = "";
28
y = "2021";
29
for (i = 1; i < 32; i++) {
30
if (i > 3 && i < 21) {
31
ord = "th";
32
} else if (i == 1 || i == 21 || i == 31) {
33
ord = "st";
34
} else if (i == 2 || i == 22) {
35
ord = "nd";
36
} else if (i == 3 || i == 23) {
37
ord = "rd";
38
} else {
39
ord = "th";
40
}
41
fulldate += i + ord + " of " + mv + "" + y + "<br>";
42
}
43
}
44
document.getElementById("dates").innerHTML = fulldate;
45
}
JavaScript
1
6
1
<label for="month">SELECT MONTH</label><br>
2
<select id="month" name="month" oninput="showdates()">
3
<option value="December">December</option>
4
<option value="January">January</option>
5
</select><br>
6
<p id="dates"></p>
Advertisement
Answer
- In your script you need to move the mv inside the function BUT
- Add “please select”
- DRY Do not repeat yourself
Here is a better script
JavaScript
1
28
28
1
const nth = d => {
2
if (d > 3 && d < 21) return 'th';
3
switch (d % 10) {
4
case 1: return "st";
5
case 2: return "nd";
6
case 3: return "rd";
7
default: return "th";
8
}
9
};
10
11
const showdates = function() {
12
const mv = this.value;
13
const monthName = this.options[this.selectedIndex].text;
14
document.getElementById("dates").innerHTML = "";
15
if (!mv) return; // stop
16
const now = new Date()
17
let yyyy = now.getFullYear();
18
if (mv <= now.getMonth()) yyyy++; // next year
19
const d = new Date(yyyy, mv - 1, 1, 15, 0, 0); // normalising at 15:00
20
const lastDay = new Date(yyyy, mv, 0, 15, 0, 0, 0).getDate() // the 0th of next month
21
fullDate = [];
22
for (let i = 1; i <= lastDay; i++) {
23
fullDate.push(i + nth(i) + " of " + monthName + " " + yyyy);
24
}
25
26
document.getElementById("dates").innerHTML = fullDate.join("<br/>");
27
}
28
document.getElementById("month").addEventListener("change", showdates);
JavaScript
1
17
17
1
<label for="month">SELECT MONTH</label><br>
2
<select id="month" name="month">
3
<option value="">Please select</option>
4
<option value="12">December</option>
5
<option value="1">January</option>
6
<option value="2">February</option>
7
<option value="3">March</option>
8
<option value="4">April</option>
9
<option value="5">May</option>
10
<option value="6">June</option>
11
<option value="7">July</option>
12
<option value="8">August</option>
13
<option value="9">September</option>
14
<option value="10">October</option>
15
<option value="11">November</option>
16
</select><br>
17
<p id="dates"></p>