-3

I have googled for this and, did not find any solution.

I am trying to add Birth date (datepicker) field in woocommerce registration page.

The registration form has been created as per guidance posted here

I am using datepicker script available here

According to DatePicker documentation we have to add below code in order to make it work.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="glDatePicker.min.js"></script>

<script type="text/javascript">
    $(window).load(function()
    {
        $('input').glDatePicker();
    });
</script>

Problem is all other fields in the form start displaying calendar. This is because the javascript adds calendar to all the 'input' fields of the form.

How to solve this???

Code in functions.php ....

<p class="form-row form-row-wide">
<label for="reg_billing_birthdate"><?php _e( 'Birth Date', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_birthdate" id="reg_billing_birthdate" gldp-id="mydate" value="<?php if ( ! empty( $_POST['billing_birthdate'] ) ) esc_attr_e( $_POST['billing_birthdate'] ); ?>" />
</p>

2 Answers 2

1

Don't include jquery from any other source, it will create conflict. Bcoz jQuery is pre bundled in wordpress by default.

For your question, add an id or class to the input element then add the datepicker based on the class. For example

<input type="text" class="da_test" value="" >

<script type="text/javascript">
    $(window).load(function()
    {
        $('.da_test').glDatePicker();
    });
</script>

This will avoid datepicker to appear in every input element.

3
  • replacing with class did not work. So i tried replacing with id. This worked..... $('#reg_billing_birthdate').glDatePicker();
    – kpmrpar
    Commented Jan 3, 2016 at 12:51
  • Ok thats good. Anyway these are Web standards. You should try those before coming for wordpress. Just a debug hint. Enjoy
    – WPTC-Troop
    Commented Jan 3, 2016 at 17:56
  • hi. I did try. but because i am novice in javascripting i thought that that script would only work with 'input'. I started brainstroing as soon as it was confirmed from your answer that putting class name might work.
    – kpmrpar
    Commented Jan 6, 2016 at 10:13
0

Below changes worked....

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="glDatePicker.min.js"></script>

<script type="text/javascript">
$(window).load(function()
{
    $('#reg_billing_birthdate').glDatePicker();
});
</script>

Not the answer you're looking for? Browse other questions tagged or ask your own question.