-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-html-from-js.js
119 lines (97 loc) · 4.21 KB
/
generate-html-from-js.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
document.addEventListener('DOMContentLoaded', () => {
const dateInput = document.getElementById('dateInput');
// Create input container
const inputContainer = document.createElement('div');
inputContainer.classList.add('input-container');
dateInput.parentNode.insertBefore(inputContainer, dateInput);
inputContainer.appendChild(dateInput);
// Create calendar container
const calendarContainer = document.createElement('div');
calendarContainer.classList.add('calendar-container');
calendarContainer.id = 'calendarContainer';
inputContainer.appendChild(calendarContainer);
// Create calendar header
const calendarHeader = document.createElement('div');
calendarHeader.classList.add('calendar-header');
calendarContainer.appendChild(calendarHeader);
const prevMonth = document.createElement('button');
prevMonth.id = 'prevMonth';
prevMonth.innerHTML = '<';
calendarHeader.appendChild(prevMonth);
const monthYear = document.createElement('div');
monthYear.id = 'monthYear';
calendarHeader.appendChild(monthYear);
const nextMonth = document.createElement('button');
nextMonth.id = 'nextMonth';
nextMonth.innerHTML = '>';
calendarHeader.appendChild(nextMonth);
// Create calendar body
const calendarBody = document.createElement('div');
calendarBody.classList.add('calendar-body');
calendarContainer.appendChild(calendarBody);
// Create calendar weekdays
const calendarWeekdays = document.createElement('div');
calendarWeekdays.classList.add('calendar-weekdays');
calendarBody.appendChild(calendarWeekdays);
['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'].forEach(day => {
const dayDiv = document.createElement('div');
dayDiv.textContent = day;
calendarWeekdays.appendChild(dayDiv);
});
// Create calendar dates
const calendarDates = document.createElement('div');
calendarDates.classList.add('calendar-dates');
calendarDates.id = 'calendarDates';
calendarBody.appendChild(calendarDates);
let currentMonth = new Date().getMonth();
let currentYear = new Date().getFullYear();
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
function renderCalendar(month, year) {
const firstDay = new Date(year, month, 1).getDay();
const lastDate = new Date(year, month + 1, 0).getDate();
calendarDates.innerHTML = '';
monthYear.textContent = `${months[month]} ${year}`;
// Adjust for starting the week on Monday
const adjustedFirstDay = (firstDay === 0 ? 6 : firstDay - 1);
for (let i = 0; i < adjustedFirstDay; i++) {
const emptyDiv = document.createElement('div');
calendarDates.appendChild(emptyDiv);
}
for (let date = 1; date <= lastDate; date++) {
const dateDiv = document.createElement('div');
dateDiv.textContent = date;
dateDiv.addEventListener('click', () => {
document.querySelectorAll('.calendar-dates div').forEach(d => d.classList.remove('selected'));
dateDiv.classList.add('selected');
dateInput.value = `${date} ${months[month]} ${year}`;
calendarContainer.style.display = 'none';
});
calendarDates.appendChild(dateDiv);
}
}
prevMonth.addEventListener('click', () => {
currentMonth--;
if (currentMonth < 0) {
currentMonth = 11;
currentYear--;
}
renderCalendar(currentMonth, currentYear);
});
nextMonth.addEventListener('click', () => {
currentMonth++;
if (currentMonth > 11) {
currentMonth = 0;
currentYear++;
}
renderCalendar(currentMonth, currentYear);
});
dateInput.addEventListener('focus', () => {
calendarContainer.style.display = 'block';
});
document.addEventListener('click', (event) => {
if (!calendarContainer.contains(event.target) && event.target !== dateInput) {
calendarContainer.style.display = 'none';
}
});
renderCalendar(currentMonth, currentYear);
});