I am creating a calendar application using javascript. The calendar view is displayed using javascript (dom element) for which I have created a common method and I call the same for displaying the updated calendar view. When I'm clicking on a particular date, I need to update the display of the calendar, but it's getting updated only once when the event of the update is triggered. I have created a similar demo and am providing the same below.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).ready(() => {
this.show();
for (let i = 0; i < 10; i++) {
$('#li-' + i).click(() => {
alert(i);
this.show();
});
}
});
function show() {
let elem = document.getElementById('body');
elem.innerHTML = "";
let ul = document.createElement('ul');
for (let i = 0; i < 10; i++) {
let li = document.createElement('li');
let text = document.createTextNode(i);
li.appendChild(text);
li.setAttribute('id', 'li-' + i);
li.style.cursor = 'pointer';
ul.appendChild(li);
elem.appendChild(ul);
}
}
</script>
</head>
<body>
<div id="body"></div>
</body>
</html>
On this example, when I click on a 'li' element the click event is triggered and the alert box is shown. Also, the 'show()' method gets invoked for updating the view. However, when the another 'li' element is clicked, the event is not getting triggered.