Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire jQuery UI DatePicker onclick?

The standard code for DatePicker is:

<script>
    $(function() {
       $( "#datepicker" ).datepicker();
    });
</script>
<p>Date: <input type="text" id="datepicker"></p>

How to launch datepicker onclick event from inline HTML, I mean:

<input ... onclick="" >

and with no separate JS code?

I tried:

<p>Date: <input type="text" id="datepicker" onclick="$(function() {$('#datepicker').datepicker();});"></p>

But it's working only on second click.

How to make it work on first click?

like image 952
ihtus Avatar asked Mar 01 '12 01:03

ihtus


3 Answers

After you create datepicker, open it this way:

 $('#datepicker').datepicker('show')

Go to datepicker API docs look at methods tab, it's all there

like image 80
charlietfl Avatar answered Nov 14 '22 02:11

charlietfl


<p>Date: <input type="text" id="dp" onclick="$('#dp').datepicker();$('#dp').datepicker('show');"></p>​

That work by any chance?

I'm no expert, but I believe you were having to click it twice because the first click merely initialised it as a datepicker. After this initialising, it's behaving as a normal datepicker, regardless of the onclick.

like image 38
Nick Avatar answered Nov 14 '22 03:11

Nick


Try this code:

<script>
$(function() {
    $("#datepic").datepicker({ showOn: 'button',buttonImage: 'image/calendar.gif'});

});
</script>

<input type="text" id="datepic" /> 
like image 1
user3024508 Avatar answered Nov 14 '22 02:11

user3024508