In jQuery fullcalendar we have previous and next buttons. How can we call some events on click of these buttons?
In jQuery fullcalendar we have previous and next buttons. How can we call some events on click of these buttons?
I see there are other working answers, but IMHO the simplest and more correct - at least using FullCalendar v.4 - is to intercept prev and next is to deal them in the same way of custom buttons.
Here my setup (using toastr just for demo purposes)
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
plugins: [ 'dayGrid', 'timeGrid' ],
header: {
left: 'dayGridMonth,timeGridWeek,timeGridDay',
center: 'title',
right: 'prev,next'
},
footer: {
left: '',
center: '',
right: 'prev,next'
},
customButtons: {
prev: {
text: 'Prev',
click: function() {
// so something before
toastr.warning("PREV button is going to be executed")
// do the original command
calendar.prev();
// do something after
toastr.warning("PREV button executed")
}
},
next: {
text: 'Next',
click: function() {
// so something before
toastr.success("NEXT button is going to be executed")
// do the original command
calendar.next();
// do something after
toastr.success("NEXT button executed")
}
},
}
});
calendar.render();
});
See a Codepen here
SOLUTION IN FULLCALENDAR VERSION 5:
If you're using version 5 of FullCalendar, I will suggest u to use the datesSet instead of using the .click().
datesSet: function() {
myFunction();
}
The myFunction() will be called every time when the date range of calendar has been changed or initialised. In other word, when u click on the provided prev/next buttons in the calendar, the calendar's date range will be changed and the myFunction() will be called.
reference: https://fullcalendar.io/docs/datesSet
If u really want to go with the .click() way, the following code is working for version 5 of FullCalendar.
$('.fc-prev-button').click(function(){
myFunction();
});
$('.fc-next-button').click(function(){
myFunction();
});
Both methods are perfectly work in my project (VERSION 5 OF FULLCALENDAR), hope this can help people who are struggling with the issue.
Version 3 the answer is to use:
viewRender: function (event, element, view){
// you code here
}
Version 4: https://fullcalendar.io/docs/v4/datesRender
datesRender: function (){
// you code here
}
https://fullcalendar.io/docs/v4/upgrading-from-v3
"viewRender Renamed to datesRender. Parameters have changed."
Version 5 Use :
datesSet: function (){
// you code here
}
"datesRender changed to: datesSet - renamed from datesRender. Called after a view's dates are initialized or when they change." https://fullcalendar.io/docs/upgrading-from-v4
Instead of click use on
$('body').on('click', '.fc-prev-button', function() {
});
$('body').on('click', '.fc-next-button', function() {
});
this is useful when your calendar can dynamically initialize at any point of time.
If you didn't make it with the answers above, try this:
$('span.fc-button.fc-button-prev.fc-state-default.fc-corner-left').click(function() {
alert('prev is clicked, do something');
});
$('span.fc-button.fc-button-next.fc-state-default.fc-corner-right').click(function() {
alert('next is clicked, do something');
});
FullCalendar v3.4.0
Prev, next. today, week, month buttons:
$(document).ready(function(){
$('#mycalendar').fullCalendar({
$('.fc-prev-button').click(function(){
alert("Clicked prev button");
});
$('.fc-next-button').click(function(){
alert("Clicked next button");
});
$('.fc-today-button').click(function(){
alert('Today button clicked, do something');
});
$('.fc-listWeek-button').click(function(){
alert('list week clicked, do something');
});
$('.fc-listMonth-button').click(function(){
alert('list month clicked, do something');
});
});
});