Click here to Skip to main content
15,921,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The problem i am having is that when i click a date the month doesn't appear on calendar it goes back to the default month. Example: i clicked on 21 of September and when the action is done the calendar refreshes back to July(default month).

What I have tried:

CodePen jsCalendar Example
Posted
Updated 4-Jul-17 11:36am

1 solution

When you click on the <a> for a particular day, you execute the dayClicked function to update the calendar. But you don't prevent the default action, which is to navigate to the href, which reloads the page.

You could remove the href, which would prevent the navigation. But this would also affect the style of the link:
$("#days").append("<td class='day_active'><a onclick='dayClicked(" + month + "," + year + ");'>" + i + "</a></td>");

Or, you could add return false; after calling the dayClicked function:
$("#days").append("<td class='day_active'><a href='' onclick='dayClicked(" + month + "," + year + ");return false;'>" + i + "</a></td>");

Or, you could use jQuery delegated events:
function days(month, year) {
    ...
        $("#days").append("<td class='day_active'><a href='' data-month='" + month + "' data-year='" + year + "'>" + i + "</a></td>");
    }
};

$("#days").on("click", ".day_active a", function(evt){
    evt.preventDefault();
    
    var month = this.getAttribute("data-month");
    var year = this.getAttribute("data-year");
    days(month, year);
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900