1

I'm trying to use the datepicker function but I'm getting a error.

There is a lot of topic about this problem but every time i try a different method, it doesn't work (with .noconflict() -> maybe i'm using it wrong).

I would like to change the start date and the end date to the current date when I change the button.

I'm starting JS, can you help me ? May be an other function ? pls

package.json :

{
    "devDependencies": {
        ...
        "jquery": "^3.5.1",
        ...
    }
    "dependencies": {
        ...
        "jquery-ui": "^1.12.1",
        ...
    }
}

JS code :

const $ = require('jquery');

$( document ).ready( function() {

    $('.form-check').on('change', function () {
        if( $("[type='radio']:checked").val() == '1')
        {
            $('.form-period').show();
            $('.form-date').hide();
        } else {
            $('.form-period').hide();
            $('.form-date').show();
        }
        $('.js-datepicker').datepicker("setDate", new Date());
        $('.alert').remove();
    });

});

Console :

Uncaught TypeError: $(...).datepicker is not a function

Edit : HTML is generated by the symfony code :

->add('searchStartAt', DateType::class, [
                'label' => 'Date de début',
                'widget' => 'single_text',
                'required' => false,
                'data' => new \DateTime('now'),
                'attr' => ['class' => 'js-datepicker']
            ])
            ->add('searchEndAt', DateType::class, [
                'label' => 'Date de fin',
                'widget' => 'single_text',
                'required' => false,
                'data' => new \DateTime('now'),
                'attr' => ['class' => 'js-datepicker']
            ])

End of my file html.twig :

{% block javascripts %}
    {{ parent() }}
    {{ encore_entry_script_tags('choice_form_stats') }}
{% endblock %}

webpack.config.js :

var Encore = require('@symfony/webpack-encore');

Encore
      .addEntry('choice_form_stats', './assets/choice_form_stats.js')
      ...
;

module.exports = Encore.getWebpackConfig();

app.js :

const $ = require('jquery');
require('bootstrap');
require('jquery-ui');

thanks,

4
  • add your HTML code too Commented May 9, 2021 at 18:49
  • Welcome to Stack Overflow. This question has been asked and addressed. You must initialize DatePicker before you can use setDate method. Simply add $('.js-datepicker').datepicker(); to the beginning of your Ready statement.
    – Twisty
    Commented May 9, 2021 at 20:22
  • Using $('.js-datepicker').datepicker(); to initialize, it doesn't work => Console warning : jQuery.Deferred exception: $(...).datepicker is not a function TypeError: $(...).datepicker is not a function
    – dyd
    Commented May 10, 2021 at 6:57
  • @dyd please check your <HEAD> and <SCRIPT> sections. It sounds like you are not loading the jQuery UI library.
    – Twisty
    Commented May 10, 2021 at 14:18

0