Customizing datepicker colors for specific dates

I'm attempting to modify the color of selected dates in jQuery Datepicker using CSS and !important.

The beforeShowDay function (f1) is called to check if days are included in arrays (t1, t2, and t3), and if a day appears in two different arrays (like 10-6-2016), the second occurrence is ignored.

What I want is for the colors to change when a day is found in another array. Can anyone provide assistance?

jQuery

function f1(date){
    t1=[ '10-6-2016','11-6-2016'];
    t2=['15-6-2016', '16-6-2016'];
    t3=['10-6-2016','20-6-2016','21-6-2016'];
    dmy=date.getDate() + "-" +(date.getMonth() + 1) + "-" + date.getFullYear();
    if($.inArray(dmy,t1) !==-1){return[true,'tt1','selectable'];} // check for date
    if($.inArray(dmy,t3) !==-1){return[true,'tt3','selectable'];} // check for datey
    if($.inArray(dmy,t2) !==-1){return[false,'tt2','unselectable'];} // check for date
    else{return[true,"","selectable"];}
}

CSS

.tt1  a{
    background-image:none !important;
    background:red !important;
    color:#fff !important;
    opacity:100 !important;

}
.tt2 span{
    background:blue !important ;
    color:#fff !important;
    opacity:100 !important; 
}
.tt3  a{
    background-image:none !important;
    background:green!important;
    color:#fff !important;
    opacity:100 !important;

Answer №1

When the return statement is reached in your function, it immediately returns an array if the first if condition is met.
This action also stops the function execution.

UPDATE:

To see the console logs, you can try this code snippet:

function f1(date){
    t1=['10-6-2016','11-6-2016'];
    t2=['15-6-2016','16-6-2016'];
    t3=['10-6-2016','20-6-2016','21-6-2016'];
    dmy=date.getDate() + "-" +(date.getMonth() + 1) + "-" + date.getFullYear();

    presentInt1=false;
    presentInt2=false;
    presentInt3=false;

    if($.inArray(dmy,t1) !==-1){            // Check if date is in array t1
        console.log("Date is in array t1.");
        presentInt1=true;
    }

    if($.inArray(dmy,t2) !==-1){            // Check if date is in array t2
        console.log("Date is in array t2.");
        presentInt2=true;
    }

    if($.inArray(dmy,t3) !==-1){            // Check if date is in array t3
        console.log("Date is in array t3.");
        presentInt3=true;
    }

    // You can then use the 'presentIn' boolean variables to make decisions further down in your code.
}

Answer №2

A big shoutout to L. P. Bessette for pointing me in the right direction. The datepicker method diligently scans through each day until it finds a match, which in this case was on 10-6-2016 within array t1. It then returns with true and the corresponding CSS without even needing to reach t3 in its search for 10-6-2016. To adjust for this, I could rearrange the if statements or come up with more intricate conditions. Once again, thank you for your assistance.

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

What should you do when the server is taking a while to respond?

I am in the process of creating a webpage that involves interactions between users, and I could use some guidance. Let's consider this hypothetical scenario: Client A visits a 'public' webpage and clicks a button. Client A then waits for a ...

What is the method for attaching a keypress event to an HTML document?

Looking to add an interactive touch to my website by creating a "press any key" page. When a key is pressed, I want it to kick off animations that bring the page to life - like sliding elements in from different directions. Open to using jQuery or plain ...

The title of the Electron application remains consistent

My application is being packaged using electron-packager, but it's not changing its name and still displays "Electron." It's supposed to use the productName in my package.json file, but for some reason, it doesn't change. Even after creati ...

What is the best method for effectively organizing and storing DOM elements alongside their related objects?

In order to efficiently handle key input events for multiple textareas on the page, I have decided to create a TextareaState object to cache information related to each textarea. This includes data such as whether changes have been made and the previous co ...

How can I configure Select2 to begin searching alphabetically?

I'm currently using select2 for tag searching within my article tracking application, and I've noticed an issue. When typing 'm' or 'mi', the expected behavior would be for tags like 'migrations' to appear first. How ...

Do custom Bootstrap 4 variables fail to cascade downwards?

Discovering a strange behavior in the custom.scss file of Bootstrap, I realized that setting a custom color and removing the !default flag doesn't cascade down as expected. For example, updating $blue to #000 without !default in _custom.scss should id ...

Maintaining hover effects even when elements are not in view

I am facing an issue with my absolutely positioned <div> (which serves as a menu) located in the corner of a webpage. The problem arises when I try to animate it on hover, but as soon as the cursor moves beyond the viewport, the hover action stops. I ...

What is the best method for integrating static files (such as css, images, js, etc.) into our Node.js application?

Here is the structure of my project folder: XYZ_PROJECT_FOLDER ASSETS CSS IMAGES JS VENDOR CONTACT.html INDEX.html INDEX.js In the INDEX.js file, I have included code to render all the static files and routing logic: const e ...

PHP Code for Removing Selected Entries

Is it possible to delete specific records from a table? For example, if I have records in my table like this: Monday | Tuesday | Wednesday IT | IT | IT When I delete "IT" from any day, all instances of "IT" are deleted from all days. However, I ...

What steps are involved in linking Java with JavaScript?

After attempting to connect html and javascript using applet, unfortunately it was unsuccessful. I am curious if there is an alternative method besides applet to establish this connection. ...

Unable to execute two functions consecutively

After running the code, I encountered the error message Uncaught TypeError: object is not a function and only the status function is working properly. Can anyone help me identify the issue? $(document).ready(function() { steam('status', 60 ...

Using Firebase collection inside an Angular constant

Currently, I am working on a Datatable using ng-bootstrap that utilizes a static array to display data. There are 3 main files involved: In the "country.ts" file, the necessary values are declared: export interface Country { id: number; name: string; ...

Obtain an HTML element using JavaScript

What is the best method to access the top-left cell of a table using JavaScript from a specified URL? For instance, in this URL: https://www.w3schools.com/jsref/dom_obj_table.asp In the 'Table Object Methods' section, I am interested in retriev ...

PHP PDO and MySql are used for creating a DataTable that can perform CRUD operations on the server

I am currently attempting to retrieve data from a child table using the ID stored in the child table. The goal is to populate a DataTable with records related to both the Parent and Child tables. Below you can find the code snippet: The issue arises when ...

When a user connects to Node.js using sockets, the previous messages are loaded. However, a bug causes the messages to be loaded to all chat users, instead of just the

New to node.js, I am currently creating a chat application with two main files: server.js (server side) and script.js (client side). In the server.js file: socket.on('previousMessages', function (data){ db.query("SELECT * FROM messages", f ...

Gravity forms: AJAX-loaded checkboxes become unchecked due to validation errors

Gravity Forms is being used to dynamically load checkboxes based on a dropdown selection through AJAX. Everything functions correctly until the form is submitted with invalid data (such as leaving a required field empty), causing all checkboxes to become u ...

What is the reason for the lack of letter-spacing between a nested inline element and the next character that follows?

Upon reviewing the specifications for letter-spacing, it is evident that runs of atomic inlines (e.g. inline-block) are considered as a single character (http://www.w3.org/TR/css3-text/#letter-spacing): For letter-spacing purposes, each consecutive run ...

Is there a way to create a div that resembles a box similar to the one shown in the

Hello, I am attempting to achieve a specific effect on my webpage. When the page loads, I would like a popup to appear at the center of the screen. To accomplish this, I have created a div element and initially set its display property to 'none'. ...

The step-by-step guide on passing arguments and fetching results from Angular UI Bootstrap Modal through Components

I am facing a simple scenario where I have defined a modal as a component and opened that modal using `uiModal.open`. However, when I try to pass custom data to the modal using "resolve" in the open method and arguments in the controller constructor, the d ...

Encountering: Unable to break down the property 'DynamicServerError' of 'serverHooks' as it does not have a defined value

An error has arisen in a Nextjs app with TypeScript, specifically in the line of my react component which can be found here. This is my inaugural package creation and after several trials, I managed to test it successfully in a similar vite and TypeScript ...