calendar: how to prefill a calendar field with current date?

  • rd
  • Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 58
  • Thank you received: 2

rd created the topic: calendar: how to prefill a calendar field with current date?

Hello,

how can a responsive Calendar field (name: calendar) be prefilled with the current Date upon initialization.

The functionality of the calendar should be kept. But the user don't have to define the current date (but is able to change it if neccesary)

Thank You.
Regards
Rolf
#235193
  • Posts: 2109
  • Karma: 72
  • Thank you received: 267

mihaela replied the topic: calendar: how to prefill a calendar field with current date?

Hello,

Here is how you can prefill the responsive calendar element with the current date.

Go to form's properties > Advanced tab > More options > Form Pieces > Before Form > Type > Custom and paste this code in the code area:
echo '
<script type="text/javascript">
jQuery(document).ready(function () {
var myDate = new Date();
var todayDate =myDate.getFullYear() + "-" + (myDate.getMonth()+1) + "-"  + myDate.getDate();
jQuery("#ff_elem122").val(todayDate);
});
</script>';
NOTE: You have to replace ff_elem122 with the ID of your calendar element in order for this to work.

The code above is meant for responsive calendar element with "yyyy-mm-dd" date format.

Let me know whether this works as you wanted.

Regards,

Mihaela
#235201
  • rd
  • Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 58
  • Thank you received: 2

rd replied the topic: calendar: how to prefill a calendar field with current date?

Hello Mihaela,

great -- works perfect!
please let me ask one more small Question:
I rearranged the date format to dd.mm.yyyy. The Date is displayed like: 2.4.2019
How can i display the leading 0 like so: 02.04.2019

Thank you
Regards
Rolf
#235206
  • Posts: 2109
  • Karma: 72
  • Thank you received: 267

mihaela replied the topic: calendar: how to prefill a calendar field with current date?

Hello again,

In that case, replace this line of code:
var myDate = new Date();
var todayDate =myDate.getFullYear() + "-" + (myDate.getMonth()+1) + "-"  + myDate.getDate();

with this one:
var todayDate = ("0" + myDate.getDate()).slice(-2) + "." + ("0" + (myDate.getMonth()+1)).slice(-2) +"."+  myDate.getFullYear() ;

That should do the trick. Let me know whether it worked.

Regards,

Mihaela
#235208
The following user(s) said Thank You: rd
  • rd
  • Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 58
  • Thank you received: 2

rd replied the topic: calendar: how to prefill a calendar field with current date?

Hello again,

yes, thats it. but not I run into a new issue.
I did not tell you, that this form is maintained by ContentBuilder and that the user can edit the form later on. So It is working perfect but if the user has not taken the current date for any reason, saved the form and comes back to change data the date element jumps back to the current date.

What can we do to avoid this. So, in other words, On the FIRST load the date is prefilled with the current date, on the next load via "edit form" the date stays on the date that has been saved previously.

Is this possible?

Regards
Rolf
#235209
Moderators: ForumSupporttomeperica
Time to create page: 0.064 seconds

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here

News and Updates

Get informed about new downloads, updates and more in our News and Updates newsletter.

All Extensions Subscription

Get 1 year access to all of our current and future products and 1 year of professional support -- 99€

No support per domain or website installation limits! Includes all of our current and future Joomla!® extensions, Joomla!® templates for the duration of your membership. This means, by purchasing an All Extensions Subscription you'll have it all covered!

Get it from here

3rd Party Discount - 25% Off

We help you to keep your costs under control. If you are a new member and purchased a form building tool from a different form vendor, then you'll get a 25% discount on our subscription plans.

How to receive the discount:

Send us a quick email to sales@crosstec.org with a proof of purchase (for example a paypal receipt), await payment instructions and enjoy your membership!

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here