2

Thought it would be more easier than it is. Task:

1)disable future days. (Set maxDate as today - done)

2)from js variable get date and set it as a minDate on the fly - how?

main function (which I use for few pages):

jQuery(
    function($){
        $('#datepicker').datepicker({
             onSelect: function(dateText, inst){calendarOptions(dateText);},
            // beforeShow: function() {$("#datepicker").datepicker( "option", "minDate", curdate ).next('button').text('Kalendārs').button({icons:{primary : 'ui-icon-plus'}})}, 
             showOn: "button",
             dateFormat: 'dd.mm.yy',
             prevText: '«',
             nextText: '»',
             monthNames: ['Janvāris','Februāris','Marts','Aprīlis','Maijs','Jūnijs', 'Jūlijs','Augusts','Septembris','Oktobris','Novembris','Decembris'],
             monthNamesShort: ['JAN', 'FEB', 'MAR', 'APR', 'MAI', 'JUN', 'JUL', 'AUG', 'SEP', 'OKT', 'NOV', 'DEC'],
             dayNamesMin: ['S', 'P','O','T','C','P','S'],
             firstDay: 1,
             maxDate: calendarMaxDate()
        }).next('button').text('Kalendārs').button({icons:{primary : 'ui-icon-plus'}});
    }

);

And then comes js code for page I want to edit now:

function calendarMaxDate(){//disable future days
var date = new Date(); 
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
return new Date(currentYear, currentMonth, currentDate)
//return null; // set to default
}

function calendarOptions(dateText){ // sets selected date to 'external' input field
autofield_date.setValue(dateText);
}

jQuery(
    function($){ // calls function before ui datepicker button is pressed
        $('.ui-datepicker-trigger').click(function() {
        alert(autofield_dekl.getValue());
            MinDate();
        });
    }
);

function MinDate(){ //pass curdate value as'minDate' to calendar on the fly

    jQuery( //not sure about this part
        function($){ //without onload function it throws an error: $("#datepicker") is null
            $("#datepicker").datepicker( "option", "minDate", curdate ).next('button').text('Kalendārs').button({icons:{primary : 'ui-icon-plus'}})//;
        }
    )
    alert(curdate);
}

function MinDate() works only in the first time when the button has been pressed.

karlisup
  • 672
  • 12
  • 22
  • what type of project is this for? ASP.NET or MVC for example? – Luke Duddridge Feb 25 '11 at 13:09
  • Strange way subinitialization event onload in MinDate. So it should be? – aavezel Feb 25 '11 at 13:10
  • @Luke Duddridge -It's just PHP/Javascript/Ajax/HTML/SQL mix ;). @aavezel Are you talking about 'function($){ }'? Im quite new to jquery and I'm doing things as I can not as they should be. – karlisup Feb 25 '11 at 13:30
  • Just removed 'function($){ ... }' and at least now the function alerts values every time when the button has been pressed - now, only problem has left is that datepicker doesn't change minDate value after function has been called. – karlisup Feb 25 '11 at 13:39
  • Still problem isn't solved - for now I have made alternative, which works but doesn't look good. Thanks everyone for comments. Anyway, if anyone can offer a solution that fits to problem I would be happy to see it. – karlisup Feb 28 '11 at 07:34

1 Answers1

4

i found a possible answer on this site

apparently you use the option value to do so:

 $("#datepicker").datepicker('option', 'minDate', newDate); 
zeph
  • 51
  • 2
  • 1
    As seen in the OP's code, he is already attempting to set the option and says it is not working. – JAAulde Aug 12 '11 at 15:08