Javascript To Set Defaultdate Not Getting Executed

I’ve spent way too long on this and I’m going nowhere so I’m here to ask for help.

All I want to do is execute some JS to so that the Juidateselector is pre-populated.

I’m using the following code:


    $("#start_date_visible").datepicker('setDate', new Date());


When I enter it in the FF console after the page has loaded it works as intended. However, it does not work when I try to use it inline:

<script type="text/javascript">


        $("#start_date_visible").datepicker('setDate', new Date());



or when I put it into an extra JS file and load the JS in the layout/main.php or in the view that calls the renderPartial. The field I’m trying to populate is created as part of this call:




Is that the source of my troubles? I’m totally lost and would really appreciate some pointers.

Thanks for taking the time

First, you should register that on page load by wrapping it in jQuery.document(‘ready’, function(){ YOUR_CODE });

Yii can register a script like that for you, just use Yii::app()->clientScript->registerScript() with the last argument set to CClientScript::POS_READY.

Then again, you can just set a default value when calling JuiDatePicker widget.

I thought $(function())} was the short form for jQuery.document(‘ready’…?!?

The default option for the date picker seems to only highlight a certain date in the graphical popup rather than populating the relevant text box.

However, your suggestion with clientScript->registerScript worked. Yay!

This is what my code now looks like:

Yii::app()->clientScript->registerScript('dateselect', "


        $('#start_date_visible').datepicker('setDate', new Date());

        $('#end_date_visible').datepicker('setDate', new Date());


", CClientScript::POS_READY);

Thanks for your help!