Search code examples
javascriptfullcalendarfullcalendar-4

How to Change all FullCalendar 's events color onClick?


I want to change the color of the selected event onClick , in addition when i select another event in my calendar i want the previous event to get it's initial color.

this is what i tried :

eventClick:function(info){

       
    if($(this).css('background-color')=='rgb(58, 135, 173)'){
            $(this).css('background-color','red');
       }
                                                 
   else{
     $(this).css('background-color','rgb(58, 135, 173)');
       }
  }

i tried to add this line before the if block , but nothing has changed :

$('#calendar').fullCalendar(" eventColor", "#378006");                       

so any idea ?

Update

i find in docs that i can set dynamically the options of the calendar ,i did that:

$('#calendar').setOption('eventColor','blue')

but i get an error telling that setOption isn't a function ?


Solution

  • I get the answer here , just add a variable to store the previous event and change its background on next Click :

            var previousClickedEvent;
           $('#calendar').fullCalendar({
                           header: {
                           left: 'prev,next today',
                           center: 'title',
                           right: 'month,agendaWeek,agendaDay'
                            },
                             locale:'fr',
                             defaultDate: '2016-10-01',
                             navLinks: true,
                             eventLimit: true,
                             events:events,
    
                             eventClick:function(info){                                                               
                                 if(previousClickedEvent){
    
                                        previousClickedEvent.css('background-color','rgb(58, 135, 173)');
                                    }
    
                                      /*some code*/
    
    
                                  if($(this).css('background-color')=='rgb(58, 135, 173)'){
    
                                       $(this).css('background-color','red');
                                   }
                                  else{
    
                                      $(this).css('background-color','rgb(58, 135, 173)');
                                   }                                   
                              previousClickedEvent=$(this);}})