Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Texas's avatar

date picker

Hi,

I'm trying to implement the datepicker from boostrap... if anyone has done it before, I'd like some advice on how to do it.

I have this in my view:

Date: <input type="text" id="datepicker" size="30">

and this:

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/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>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
    $( "#datepicker" ).datepicker();
  });
  </script>

The result is a input element and if I click on it, nothing happens.

Am I missing something ?

Thanks.

0 likes
17 replies
Snapey's avatar

You mean the datepicker from jQuery-UI ?

Make sure your CSS is in the < head >

Make sure the javascript is at the bottom

Open developer tools in your browser, network tab, reload the page. Look for any red entries

Switch to the console tab (in your browser), refresh the page and check for any messages.

Click on the field and check for messages.

Texas's avatar

I get this:

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

Snapey's avatar

It should be if all the files have loaded? No network errors?

Texas's avatar

I got a 404 here

 <link rel="stylesheet" href="/resources/demos/style.css">
postitief's avatar

The error means that your function for the datepicker hasn't been loaded. So you make a call to the datepicker but the js files isn't loaded jet. Or you are including the wrong js files, the ones that have no datepicker function!

Snapey's avatar

Thats fine, its just a stylesheet used on the jquery-UI website that you don't have on your own installation.

It's just used to style the demo - so thats not the problem

Snapey's avatar

As your code is verbatim from the jquery-ui website, I would expect it to work.

Can't tell if you have other things on the page affecting its loading. Can you paste the whole layout file?

Texas's avatar

The file is to large to paste it here. But to keep things simple.. the link rel, link href, script tags and jquery function are in a layout.blade.php file The <input type="text" id="datepicker" size="30"> is in my view that extends the layout. I tried to put them all in the view, but still nothing new happens.

It seems to complicated, so I think I'm going to drop this idea and work on a date validator myself

Snapey's avatar

We are having to take on trust that the files are being loaded as you say.

Hopefully you will see in the network log, loading of jquery and the jqueryUI plugin

The problem boils down to the name of the datepicker not being recognised which means jquery-UI is not loaded.

We know jquery is loaded else there would be an error with the $ element

1 like
Texas's avatar

@Snapey - if it's not too much, could you please list here the steps i need to follow in order to make this work ? Please take into account that i have a view.blade.php that extends layout.blade.php

Thanks.

vitorarjol's avatar

@Texas Maybe the assets aren't load on the execution of your function. Try this instead:

$( document ).ready(function() {
    $( "#datepicker" ).datepicker();
});
thomaskim's avatar

@vitorarjol That is the same thing as what he already has. $(function() { is the shortened version of $( document ).ready(function() {.

@Texas It is infinitely easier to help you after seeing what your code looks like. With only snippets, we can only guess as to what's going on.

1 like
Texas's avatar

OK. I'll try to be as specific as I can. There are 2 files involved: layout1.blade.php and advancedsearch.blade.php. The second one extends the first one. These are rather large and I can't put them all here.

In of layout1.blade.php I have:

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/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>
  <link rel="stylesheet" href="/resources/demos/style.css">

At the bottom I have:

<script>
  $(function() {
    $( "#datepicker" ).datepicker();
  });
  </script>

In advancedsearch.blade.php I have:

{!!Form::open()!!}
   {!!Form::input('text', 'StartDate', ["id"=>"datepicker"])!!}
{!!Form::close()!!}

The result is that I see the input field, but when I click on it, nothing happens. And the little icon at the right of the input field is not shown either. I'm quite sure that I didn't understand all the steps involved....

skliche's avatar

@Texas Have a look at the html code of the rendered page in the browser. Do you actually see all that stuff there? Otherwise you might have a problem that your layout is not picked up.

If I just paste that stuff in a simple view and have that returned it works like a charm:
routes.php:

Route::get('datepicker', function(){
    return view( 'datepicker' );
});

datepicker.blade.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Datepicker</title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/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>
</head>
<body>
    <form>
        <input type="text" name="startDate" id="datepicker">
    </form>
<script>
    $(function() {
        $( "#datepicker" ).datepicker();
    });
</script>
</body>
</html>
1 like
Snapey's avatar

when you get datepicker is not a function, does this happen immediately on page load or just when you click the date field?

Texas's avatar

@Snapey - the problem WAS happening in page load... "was" because I fixed it in the meantime. Based on the example shown by skliche , in advancedsearch.blade.php right next to the input field I did this:

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
                <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
                <span>{!!Form::text('StartDate',null, array('id'=>'datepicker'))!!}</span><br />
                <script>
                    $(function() {
                        $( "#datepicker" ).datepicker();
                    });
                </script>

(..I changed the input field to text) So I took every <script> from layout.blade.php and I added right were action should be... Although it seems a little extreme to me... So thank you all for your help... one last thing: how can I change the date format ?

rmariuzzo's avatar

@Texas, if you are trying to ‘implement the datepicker from Bootstrap’ as you mentioned and not from jQuery UI, you should add the required dependencies. You will need jQuery, Bootstrap and Bootstrap DatePicker. In your code, you already have jQuery then just download both Bootstrap and Bootstrap DatePicker (not sure which implementation) then reference it in your HTML.

Can you confirm what are you looking for, jQuery UI's DatePicker or any Twitter Bootstrap's DatePicker?

Please or to participate in this conversation.