15

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?

3
  • Here's an answer of how to do that stackoverflow.com/a/1127215/1078487 Commented Mar 1, 2012 at 1:14
  • thanks. I have just updated my q: How to launch datepicker onclick event from inline html <input ... onclick="" , and with no separate JS code? Commented Mar 1, 2012 at 1:18
  • not a good idea in the first place and there can't possibly be any reason it has to be an inline onclick with no other datepicker code in page Commented Mar 1, 2012 at 1:34

4 Answers 4

12
<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.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks! that worked! from docs: datepicker( "show" ) - Call up a previously attached date picker. So "$('#dp').datepicker()" - initialization;
thanks! that worked! from docs: datepicker( "show" ) - Call up a previously attached date picker. 1. So "$('#dp').datepicker()" - initialization; 2. $('#dp').datepicker('show'); - force showing datepicker. Is that right?
Yep, that's what it's doing. I think this not best practice though, and depending on how your program works, you may want to look at the .live function in jQuery? Anyway glad it helped.
6

After you create datepicker, open it this way:

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

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

1 Comment

as you can see in the q: I know how to use it with a separate js code. My aim is to use inline onlclick="" event execution.
1

Try this code:

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

});
</script>

<input type="text" id="datepic" /> 

Comments

0

like this one?

http://jsfiddle.net/tZXjR/

1 Comment

thanks. I have just updated my q: How to launch datepicker onclick event from inline html <input ... onclick="" , and with no separate JS code?

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.