Customize your Jquery UI Calendar with Changing Background Images for Each Month!

How can I change the background of jQuery UI datepicker based on the selected month?

I have created 12 classes, each representing a month, and the selected month already has the background. I tried using "onChangeMonthYear: function(year, month, inst) { ... }" but didn't achieve the desired result.

Any suggestions or advice on how to accomplish this?

$(document).ready(function(){
  
  $('#calendar').datepicker({
    inline: true,
    firstDay: 7,
    showOtherMonths: true,
    dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
    });
 
  var month = $('.ui-datepicker-month').text().toLowerCase();
  $('.calendar_img').addClass(month);
})
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  font-family: 'Roboto', sans-serif;
  -webkit-font-smoothing: antialiased;
}

.page {
  position: absolute;
  display: table;
  width: 100%;
  height: 100%;
}
.page .page_content {
  display: table-cell;
  vertical-align: middle;
  padding: 80px 0;
}

.calendar_container {
  display: flex;
  width: 605px;
  min-height: 400px;
  box-shadow: 4px 5px 18px 0px rgba(0, 0, 0, 0.3);
  margin: 0 auto;
  border-radius: 10px;
  overflow: hidden;
}
.calendar_container .calendar_img {
  float: left;
  position: relative;
  width: 250px;
  min-height: 400px;
  background-color: #f4f8f1;
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center center;
}
.calendar_container .calendar_data {
  float:

...

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<link href='https://fonts.googleapis.com/css?family=Playfair+Display:400,700|Roboto:400,300,500,700,900' rel='stylesheet' type='text/css'>

<div class="page">
  <div class="page_content">
    <div class="calendar_container">
      <div class="calendar_img"></div>
      <div class="calendar_data">
        <div id="calendar"></div>
      </div>
    </div>
  </div>
</div>

Answer №1

Implement an onChangeMonthYear event handler to retrieve the selected month from the callback function (remember that the month is indexed starting from 0), then remove all classes from previous months and add the class of the selected month.

$(document).ready(function() {

  $('#calendar').datepicker({
    inline: true,
    firstDay: 7,
    showOtherMonths: true,
    dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
    onChangeMonthYear: changeBackground

  });


  function changeBackground(year, month, obj) {
    var months = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"
    ];

    months.forEach(function(value) {
      $('.calendar_img').removeClass(value);
    });

    $('.calendar_img').addClass(months[month - 1]);
  }

  var month = $('.ui-datepicker-month').text().toLowerCase();
  $('.calendar_img').addClass(month);

})

Do not use

$('.ui-datepicker-month').text().toLowerCase()
in the callback function because it gets triggered before the widget changes the value.

Check out the code on JSFiddle

Answer №2

Integrate a change event into the datepicker component.

 $('#calendar').datepicker({
    inline: true,
    firstDay: 7,
    showOtherMonths: true,
    dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
    onChangeMonthYear: adjustBackground
    });

function adjustBackground(year, month, widget){
 var month = $('.ui-datepicker-month').text().toLowerCase();
 $('.calendar_img').addClass(month);
}

Answer №3

Here is the solution:

  $('#schedule').datepicker({
    inline: true,
    firstDay: 7,
    showOtherMonths: true,
    dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
    onChangeMonthYear: function(year, month, widget) {
      var month_name = $('.ui-datepicker-month').text().toLowerCase();
      $('.schedule_img').addClass(month_name);
    }
  });

$(document).ready(function(){
  
  $('#schedule').datepicker({
    inline: true,
    firstDay: 7,
    showOtherMonths: true,
    dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
    onChangeMonthYear: function(year, month, widget) {
      var month_name = $('.ui-datepicker-month').text().toLowerCase();
      $('.schedule_img').addClass(month_name);
    }
  });
 
   var month_name = $('.ui-datepicker-month').text().toLowerCase();
   $('.schedule_img').addClass(month_name);
  
});//END document.ready
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  font-family: 'Roboto', sans-serif;
  -webkit-font-smoothing: antialiased;
}

.page {
  position: absolute;
  display: table;
  width: 100%;
  height: 100%;
}
.page .page_content {
  display: table-cell;
  vertical-align: middle;
  padding: 80px 0;
}

.schedule_container {
  display: flex;
  width: 605px;
  min-height: 400px;
  box-shadow: 4px 5px 18px 0px rgba(0, 0, 0, 0.3);
  margin: 0 auto;
  border-radius: 10px;
  overflow: hidden;
}
.schedule_container .schedule_img {
  float: left;
  position: relative;
  width: 250px;
  min-height: 400px;
  background-color: #f4f8f1;
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center center;
}
.schedule_container .schedule_data {
  float: left;
}

.ui-datepicker,
.ui-datepicker table,
.ui-datepicker tr,
.ui-datepicker td,
.ui-datepicker th {
  margin: 0;
  padding: 0;
  border: none;
  border-spacing: 0;
}

.ui-datepicker {
  display: none;
  width: 355px;
  min-height: 400px;
  padding: 28px 24px;
  cursor: default;
  font-size: 14px;
  text-transform: uppercase;
}

/* Rest of CSS code omitted for brevity */

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<link href='https://fonts.googleapis.com/css?family=Playfair+Display:400,700|Roboto:400,300,500,700,900' rel='stylesheet' type='text/css'>

<div class="page">
  <div class="page_content">
    <div class="schedule_container">
      <div class="schedule_img"></div>
      <div class="schedule_data">
        <div id="schedule"></div>
      </div>
    </div>
  </div>
</div>

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

Switching from ejs format to html

I've been working on a tutorial that uses ejs, but I'm not too familiar with it. I'd like to know how to convert the server.js from using ejs to html. Here's the code snippet: app.set('view-engine', 'ejs') app.use( ...

Converting a JavaScript function to TypeScript with class-like variables inside: a step-by-step guide

During the process of converting a codebase to TypeScript, I encountered something unfamiliar. In particular, there are two functions with what appear to be class-like variables within them. The following function is one that caught my attention: const wai ...

Place a checkbox at the start of each table cell

Is there a way to add a checkbox before the text in the first cell of each row in an HTML table using jQuery? I attempted this method: $("table td:first-child").each(function() { $(this).prepend('<input type="checkbox" class="basic-kpi-row"/&g ...

What is the best way to retrieve the second to last element in a list

When using Protractor, you have convenient methods like .first() and .last() on the ElementArrayFinder: var elements = element.all(by.css(".myclass")); elements.last(); elements.first(); But what about retrieving the element that comes right before the ...

What could be causing the Error 400 message to appear when trying to upload a JSON file via an HTTP request?

This Code Snippet is Essential function makeRequest() { var dataToSend = { "username": "234zu", "subject": "qwertz", "content": "qw", "created_at": "2018-12-15 22:18:54", "updated_at": "2018-12-15 22:18:54" ...

Arrange three images both vertically and horizontally at the center of the page

I am facing an issue with aligning a button and 2 images in a row on my website. Currently, this is how it looks: https://i.stack.imgur.com/yrQB6.png Here is the HTML code I am using: <div class="row text-center"> <div class="col-md-12"> ...

Is there a method to identify if the dark theme is enabled in iBooks while working within an EPUB file?

Is there a specific code or feature that iBooks uses to enable the customization of certain sections within the book, such as changing to lighter colors when night mode is on? ...

What seems to be the issue with my snake game not loading properly?

I am having trouble getting something to display in my browser while working on this snake game. No matter how many times I check, I can't seem to find the mistake I made. Every time I refresh the browser, the screen remains blank. gameTime.html < ...

How to deal with an empty $_FILES array when there is file data present in $_POST

After reviewing multiple solutions, I noticed that they all utilize jQuery's .ajax() method. However, I have developed a more concise vanilla JS approach which has been quite successful for me. function ajax(options){ var settings = { met ...

break-word property not functioning correctly within pre tag

Each row in my dataTable triggers a modal to appear: <div id="verifyModal" class="modal"> <div class="modal-content"> <h2>Verify # <span id="verify-modal-id"></span></h2> <pre><code class="" id="verif ...

Ways to eliminate text following a string substitution

When running the code below with keys assigned to summer, spring, fall, and winter, the output for ins would be: ['req.body.summer, req.body.spring, req.body.fall, req.body.winter'] I need to eliminate the surrounding string from the replace co ...

When I click on my navbar toggle button, why isn't my text displaying?

I'm experiencing an issue with my navbar toggle. The toggle button works fine, but when I click on it, the text inside the button doesn't show up. I've checked the Bootstrap docs to make sure I didn't miss any steps, but I can't se ...

Implementing the slideToggle function with Angular directives

Hi there! I have successfully implemented the slideToggle function using Angular and jQuery. To ensure that this function only affects a specific HTML element, I am currently passing a parameter within the ng-click function. While my code is functioning as ...

Guidance on exporting an Excel file from an HTML table, excluding the final row, using JavaScript

I'm able to export an Excel file from an HTML table, but I'm facing an issue where the last row (tr) meant for pagination on the screen is also being included in the exported result. How can I exclude this row from showing up in the Excel file? ...

Firefox and Internet Explorer do not support the functionality of HTML5 <audio> tags

My radio stream player code works perfectly on Chrome and iOS Safari, but I'm facing compatibility issues with Firefox and Internet Explorer 11. Here's a snippet from my index.php file: <script type="text/javascript" src="http://code.jquery. ...

Is it possible to reset the existing localStorage value if we access the same URL on a separate window?

In my app, there are two different user roles: admin and super admin. I am looking to create a new window with a Signup link specifically for registering admins from the super admin dashboard. Is it possible to achieve this functionality in that way? Cu ...

Replacing text in the main navigation bar with sIFR

Could this menu layout be improved? <div class="navigation"> <ul id="nav-menu"> <li class="active"> <div class="sifr-r active"><a href="#" title="home" class="top-link"><span class="blue-small">01.</span& ...

I'm curious if there's a method to ensure that the content within a mat-card stays responsive

So I'm working with this mat-card: <mat-card> <mat-card-content> <div *ngFor="let siteSource of siteSources | paginate: { itemsPerPage: 5, currentPage: page};"> <site-details [site]='siteSource'></s ...

Eliminating blank attributes within an array of objects

I'm currently working on a task that involves creating an array to summarize another array. I've received valuable suggestions from various sources, including this discussion on Stack Overflow. Although the solutions provided are effective, they ...

Altering the positioning of the spinning wheel feature in jQuery

I've successfully implemented a spinning wheel feature that appears when clicking a link in jQuery UI Tabs. However, the issue is that the wheel appears next to the link instead of at the top of the div container within the tabs. The jQuery code snip ...