0

I would like to highlight some custom dates: this is what I have so far

if ( jQuery('.datepicker').length ) {
        jQuery('.datepicker').each(function (i) {
            var $item = jQuery(this);
            var fechas = $item.data('fechas');              
            var options = {
                'display' : $item.data('display') == '' ? '' : $item.data('display'),
                beforeShowDay: function(date) {
                    console.log(date);
                    if ( fechas.length ) {
                        for( var i = 0; i < fechas.length; i++ ) {
                            if ( fechas[i] == date) {
                                return [true, 'css-class-to-highlight', 'tooltipText'];
                            }
                        }
                    }
                }
            }
            $item.datepicker( options );
        });
    }

But then I got this error

Uncaught TypeError: Cannot read property '0' of undefined 

http://jsfiddle.net/34jLb8o3/2/

the thing is I want to highlight this dates, no matter the current date, I've seen answers like this one but I don't think they apply here, Does it?

Community
  • 1
  • 1
Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378

3 Answers3

0

this seem to work better : http://jsfiddle.net/34jLb8o3/8/

jQuery(function(){
    jQuery('.datepicker').each(function (i) {
                var $item = jQuery(this);
                var fechas = $item.data('fechas');              
                var options = {
                    'dateFormat' : 'dd/mm/yy',
                    'display' : $item.data('display') == '' ? '' : $item.data('display'),
                    beforeShowDay: function(date) {
                        if ( fechas.length ) {
                            var datestr = [ date.getDate(), date.getMonth()+1, date.getFullYear()];
                            datestr = datestr.join('/');
                            console.log(datestr);
                            if ($.inArray(datestr, fechas) != -1) {                                    
                                return [true, 'css-class-to-highlight', 'tooltipText'];
                            } else {
                                return [false, 'css-normal-class', 'no-highlight'];
                            }
                        }
                    }
                }
                $item.datepicker( options );
            });
});
DarkMukke
  • 2,469
  • 1
  • 23
  • 31
0

Here is the query statement to highlight date 2 nd of oct 2014

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>      
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
    <style>
        .ui-state-highlight {
        background: red !important;    
    }

    </style>
    <script type="text/javascript">
        $(function () {
            $("#date").datepicker();
            $("#date").bind('click', function () { highLightDate(2014, 9, 31); });

        })

        function highLightDate (_year,_month,_day){
            $('[data-year=' + _year + '][data-month=' + _month + '] > a:contains(' + _day + ')')
                .filter(function (index) {                   
                    return $(this).text() == _day;
                })
                .addClass('ui-state-highlight');
        }
    </script>

</head>
<body>
    <input type="text" width="100"  id="date" />
</body>
</html>

Try this code, its completely working at my end. Hope this solves your problem.

Abhishek
  • 196
  • 11
0

You can use beforeShowDay, returning an array as per the docs

http://jsbin.com/borum/2/edit?js,output

$(function() {
  var dates = ["10/10/2014","12/10/2014","13/10/2014"];              
    var options = {
      dateFormat : 'dd/mm/yy',
      beforeShowDay: function(date) {
       formattedDate = $.datepicker.formatDate('dd/mm/yy', date);   
       if(dates.indexOf(formattedDate) === -1){
          return [false];
       }
       else{
          return [true];
       }
      }
    };
  $( "#datepicker" ).datepicker( options );

});
Alex Mason
  • 76
  • 1
  • 2
  • 11