Dynamic date period

Hi,

Have a form that has a date period defined in a select drop down, at present its Fortnight, Month and 6 Month.

I have this code:

jQuery(function(){
   var dfdate=function(){return calculateDate()}();
    jQuery("#Enter_date").datepicker({
        defaultDate: dfdate,
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        beforeShowDay: jQuery.datepicker.noWeekends,
        minDate: 0
    });
})

function calculateDate(){
    var freq = jQuery('#frequency option:selected').attr('label');
    var twovalue;
    var retString;
    if(freq.indexOf(' ') >= 0){
        twovalue=  freq.split(' ');
    }
    if (freq.indexOf(' ') >= 0){
        twovalue=  freq.split(' ');
        var freqNo = twovalue[0];
        var freqPeriod = twovalue[1].charAt(0).toLowerCase();
        retString = "+"+freqNo+freqPeriod
    }else if(freq.toLowerCase() == "fortnight"){
        retString = "+2w";
    }else{
        retString = "+1"+ freq.charAt(0).toLowerCase();
    }
    return retString;
}

Open in new window


What I would like to happen is that when the selected option changes that the period defaultDate be  defined in as can be returned by the function.

How can this be done?
LVL 13
darren-w-Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
JonNormanCommented:
Try:
jQuery(function(){
    jQuery("#Enter_date").datepicker({
        defaultDate: dfdate,
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        beforeShowDay: jQuery.datepicker.noWeekends,
        minDate: 0
    });
    jQuery("#frequency").change(function(){
        var freq = jQuery(this).find("option:selected").attr("label");
        var twovalue;
        var retString;
        if(freq.indexOf(' ') >= 0){
            twovalue=  freq.split(' ');
        }
        if (freq.indexOf(' ') >= 0){
            twovalue=  freq.split(' ');
            var freqNo = twovalue[0];
            var freqPeriod = twovalue[1].charAt(0).toLowerCase();
            retString = "+"+freqNo+freqPeriod
        }else if(freq.toLowerCase() == "fortnight"){
            retString = "+2w";
        }else{
            retString = "+1"+ freq.charAt(0).toLowerCase();
        }
        jQuery("#Enter_date").datepicker("option","defaultDate",retString);
    }).change();
})

Open in new window

This assumes that everything in your function worked and this is not tested. If you have problems with this then please could you post the html as well.
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
darren-w-Author Commented:
Thats great, this was what I did.:
jQuery(function(){
    jQuery("#Enter_date").datepicker({
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        beforeShowDay: jQuery.datepicker.noWeekends,
        minDate: 0
    });
    setDate();

    jQuery("#frequency").change(function(){
        setDate();
    })
})

function setDate(){
    var freq = jQuery('#frequency option:selected').attr('label');
    var twovalue;
    var retString;
    if(freq.indexOf(' ') >= 0){
        twovalue=  freq.split(' ');
    }
    if (freq.indexOf(' ') >= 0){
        twovalue=  freq.split(' ');
        var freqNo = twovalue[0];
        var freqPeriod = twovalue[1].charAt(0).toLowerCase();
        retString = "+"+freqNo+freqPeriod
    }else if(freq.toLowerCase() == "fortnight"){
        retString = "+2w";
    }else{
        retString = "+1"+ freq.charAt(0).toLowerCase();
    }
    jQuery("#Enter_date").datepicker("option","minDate",retString);
}

Open in new window


I was after minDate actually
0
 
JonNormanCommented:
Ahh minDate makes more sense. Have you thought about what happens if they have something selected that lets them pick an early date which they then change the #frequency which means that date is invalid?
0
 
darren-w-Author Commented:
Humm, added another question, need to do more donkey work on the date http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_27828186.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.