Is it possible to customize the background color for particular days in FullCalendar?

How can I set background colors for specific days? While experimenting, I discovered this method:

$('.fc-day15').css('backgroundColor','black');

The only issue is that the colors appear on the 16th day in the calendar grid, not on the actual day. If there was a way to determine the offset from the start of the week to the first day of the month, I could adjust accordingly.

UPDATED APPROACH:

I have come up with a solution that seems to work:

var TheActualDay=15;
var DayOffset=($('#calendar').fullCalendar('getView').start.getTime()-$('#calendar').fullCalendar('getView').visStart.getTime())/(1000 * 60 * 60 * 24);
var DayNumber=(TheActualDay-1)+DayOffset;
$('.fc-day'+DayNumber.toString()).css('backgroundColor','black');

However, I am wondering if there is a more concise way to achieve the same result?

Answer №1

Here is a potential solution. Implement the viewDisplay event:

        viewDisplay: function(view) {
            if (view.name == 'month'){ //only in month view 
                var d = view.calendar.getDate(); //select the date to customize cell
                var cell = view.dateCell(d); //find location of cell for date
                var bodyCells = view.element.find('tbody').find('td');//retrieve all cells from current calendar
                var _element = bodyCells[cell.row*view.getColCnt() + cell.col]; //identify specific cell for the date
                $(_element).css('background-color', '#FAA732'); //customize the cell
            }
        }

Answer №2

Thank you to Erik Vargas for providing an insightful example. I have made a slight modification to the code in order to display the first and last day of the month in different colors.

viewDisplay: function(view) {  

                    if (view.name == 'month')
                    { //only in month view mode 
                        var start_date = view.start; // represents the first day of the current month
                        var last_date = view.end; // actually the 1st day of the next month                          
                   
                        var start_cell = view.dateCell(start_date); // obtain the cell location for the start date
                        var last_cell = view.dateCell(last_date); // obtain the cell location for the end date                  
                       
                        var bodyCells = view.element.find('tbody').find('td');// retrieve all cells from the current calendar                                                       
                        var _element_firstday = bodyCells[start_cell.row*view.getColCnt() + start_cell.col]; // get specific cell for the start date
                        var _element_lastday = bodyCells[last_cell.row*view.getColCnt() + last_cell.col]; // get specific cell for the end date

                        
                        $(_element_firstday).css('background-color', 'black'); // set the color of the cell representing the first day to black
                        $(_element_lastday).css('background-color', 'blue'); // set the color of the cell representing the last day to blue
                    }                   

        },

Answer №3

function styleEventOnCalendar(event, element, view) {          
        // One way to do it
        var dateString = $.fullCalendar.formatDate(event.start, 'yyyy-MM-dd');
        view.element.find('.fc-day[data-date="' + dateString + '"]').css('background-color', '#FAA732');

        // Another approach
        var cell = view.dateToCell(event.start);
        view.element.find('tr:eq(' + (cell.row + 1) + ') td:eq(' + cell.col + ')').css('background-color', '#FAA732');
    }

Do you have any suggestions for improving this code?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Saving the index.html file to disk when the button is clicked

Is there a way to export the current HTML page to a file? I have attempted to achieve this using the following code, but it only works when the page is loaded and not with a button click. <?php // Start buffering // ob_start(); ?> <?php file_pu ...

Exploring the Form's data-url Attribute

My form tag is set up like this: <form data-id="213" method="post" onsubmit="javascript: startAjax(); return false;"> <input type="submit" value="submit"> </form> When I submit the form, an ajax script runs to validate some information ...

The parameters sent through Ajax are coming back as undefined

I'm encountering an issue where all the data passed in my Ajax call to a PHP function is showing up as "UNDEFINED" on the server side. The JQuery debugged fine, so it seems like the problem lies within the PHP side of things. $('.js-checkout-shi ...

Using Jquery to create a slideshow with a div that is set to absolute

<!DOCTYPE html> <html> <head> <script> $(document).ready(function(){ $("#flip").click(function(){ $("#panel").slideDown("slow"); }); }); ...

Dynamic background image changes when checkbox is selected

Greetings! I am a newcomer to the stackoverflow community and I am facing an issue with dynamically changing the background image of my webpage when a checkbox is checked. Here is the HTML code snippet: <ul> <li><label for="Lines">A ...

Tips for designing a form action

I am a beginner and struggling with understanding the CSS for the code below. Can someone help me out? <div id="page-container"> <?php include_once("home_start.php"); ?> <h1>Login</h1> <for ...

Comparing two datetime objects with time zone offsets in JavaScript: How to determine if one is greater than or less than the other?

So I'm faced with a situation where I need to compare two dates where the first date is 05.01.2008 6:00 +5:00 and the second date is 05.01.2008 7:00 +5:00 I'm struggling to find a way to convert these datetimeoffsets into a specific forma ...

Callbacks for AJAX responses that are asynchronous

After using AJAX in a limited and straightforward manner for some time, I find myself currently debugging a web application that heavily relies on JavaScript and JQuery for client-side coding. One issue that has caught my attention is the possibility of mu ...

Ensuring the safety of transferring data from an unsecured website to a secured one

Is it still secure if I send an AJAX POST request from a non-secure site to a secure site? For example, if my website sends a request to https://www.google.com, is that connection any less secure than typing the URL directly into the browser? I am particu ...

Issues with margin and padding-top not functioning properly when using @media for printing

Is there a way to add some space between the top of my webpage and my logo? I've tried adjusting the padding in my CSS, but when I check the print preview in Chrome, no spacing is visible. Below is my HTML code: <div class="container"> < ...

What is causing the ajax request to override my existing comments with the newly submitted comment?

One thing I'm struggling with is this form that triggers a JS call when a comment is submitted: <%= simple_form_for([@video, @video.comments.new], :remote => true) do |f| %> <%= f.association :comment_title, :collection => @video.com ...

Unable to select image inside linked container

I'm attempting to display a dropdown menu when the user clicks on the gear-img div using jQuery. However, because it's wrapped inside an a tag, clicking redirects me to a URL. I also want the entire div to be clickable. Any suggestions for a solu ...

Error in Chrome: Uncaught TypeError with JQuery Ajax - Unable to access property 'data' when null

Trying to retrieve results from the YouTube API, my code is as shown below: function Vget(strt){ $.get("https://gdata.youtube.com/feeds/api/videos", { q: "soccer, 'start-index':strt, 'max-results':"5", v:"2",alt:"jsonc" }, functi ...

PHP ajax handling multiple requests

I've searched high and low on the internet, but couldn't find a solution to my problem. I am making multiple jQuery AJAX calls to a PHP script. Initially, I noticed that each call was being executed only after the previous one had finished. To a ...

Show the information once the ajax.load() request is finished, rather than while it is still processing

My jQuery code utilizing ajax is requesting data from a local php script called pgiproxy.php. This script fetches the desired webpage. To accomplish this, I am using the following php function: function grabPage($pageURL) { $homepage = file_get_contents ...

Tips to swap selections in a Select2 dropdown box

Is there a way to dynamically clear a Select2 option list and reload it with new data? Despite setting the data as suggested, it seems to only append the new data without clearing the existing options. $("#optioner").select2(); $("#doit").click(functio ...

Is it truly unsemantic to use transparent <hr> elements for responsive vertical spacing?

I've recently adopted a unique technique for managing vertical spacing in front-end design by using transparent <hr> elements as spacers. I understand that this method is not favored among most of the web community, but I believe it has its meri ...

Can you provide a database of words for different cities, towns, and countries in MongoDB (or in JSON

Currently, I am in the process of developing an application that utilizes MongoDB. One specific feature I am working on implementing is an 'auto-suggest' functionality on the front-end. For example, as a user begins to type the first few letters ...

Steps to display the Sidebar on top of the main information page

One unique feature of my website is the FiltersSideBar located on the left side of the page. It contains various filters to refine search results. To optimize user experience, I implemented a function that hides the sidebar at specific browser window size ...

What is the best way to utilize a variable in jQuery outside of a function?

I am attempting to utilize the .index method to determine the position of the image that is clicked on. I then need to use that number in another variable which must be external to the function. var index; $('.product img').click(function () ...