0

I'm trying to add a datepicker widget to my form on Drupal.

I've a page with the following code:

<script>
$(function() {
    $( "#datepicker" ).datepicker({
        changeMonth: true,
        changeYear: true
    });
});
</script>
<form action="/scripts/new-customer.php">
<div>Birth date: <input type="text" id="datepicker" name="bday" /></div>
</form>

But unfortunately I get the error in the subject. I tried to write as follows:

<script>
    (function($) {
        $( "#datepicker" ).datepicker({
            changeMonth: true,
            changeYear: true
        });
    })(jQuery);
</script>

But I got the same error the line after.

Unfortunately, I cannot get any further with Drupal documentation.

2
  • use jQuery.conflict() Commented Apr 20, 2013 at 17:51
  • Have you include jQuery and jQuery UI library? Commented Apr 20, 2013 at 18:06

5 Answers 5

3

Because of other JavaScript library conflict. use jQuery.noConflict()

var jq = jQuery.noConflict();

jq(function() {
  jq( "#datepicker" ).datepicker({
        changeMonth: true,
        changeYear: true
    });
});

Before initialize

drupal_add_library('system','ui.datepicker')
Sign up to request clarification or add additional context in comments.

5 Comments

Same error but with jq instead: TypeError: jq(...).datepicker is not a function.
have u include jQuery UI library
drupal_add_library('system','ui.datepicker');
check the jquery ui datepicker included or not
Thank you very much. drupal_add_library('system','ui.datepicker') solved the problem.
1

Not sure but this might help:

jQuery(function($) {
  $("#datepicker").datepicker({
    changeMonth: true,
    changeYear: true
  });
});

Comments

0

Drupal is probably using jQuery's noConflict option. Try jQuery("#datepicker").datepicker() instead.

1 Comment

Unfortunately I get TypeError: jQuery(...).datepicker is not a function.
0

To avoid conflicts with other JS libraries "$" does not make scene in .js files globally. So you should add following code surrounding js codes :

(function($) {
Drupal.behaviors.naturalessenceTheme = {
attach: function(context, settings) {

$('selector').mouseover(function() {
//now working
});

}
};
})(jQuery);

Comments

0

As suggested by Tamil, the following code is working very well:

<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/south-street/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
<script type="text/javascript" src="api/jquery.ui.datepicker-it.js"></script>
<?php drupal_add_library('system','ui.datepicker') ?>
<script>
    var jq = jQuery.noConflict();
    jq(function() {
        jq.datepicker.setDefaults(jq.datepicker.regional['it']);
        jq("#giornoSel").datepicker(
            { 
                dateFormat: 'dd/mm/yy', 
                altField: '#giorno', 
                altFormat: 'yy-mm-dd', 
                showButtonPanel: true
            }
        );
    });
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.