Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call/bind a jquery datepicker to a label or div instead of an input field

I'm working with the jqueryui datepicker on this page - http://jqueryui.com/demos/datepicker/

How do I call it on a label instead of an input field? Is this possible?

like image 392
stringo0 Avatar asked Aug 11 '09 18:08

stringo0


People also ask

How do you trigger a datepicker?

$(". trigger"). click(function(){ $("#date"). datepicker("show"); });

How datepicker is implemented in jQuery?

By default, for <input> elements, the datepicker calendar opens in a small overlay when the associated text field gains focus. For an inline calendar, simply attach the datepicker to a <div>, or <span> element.

What is MinDate and MaxDate in jQuery datepicker?

If you like to restrict access of users to select a date within a range then there is minDate and maxDate options are available in jQuery UI. Using this you can set the date range of the Datepicker. After defining these options the other days will be disabled which are not in a defined range.


2 Answers

I haven't looked at the code but I suspect that it assumes that it's attached to a <input type="text"> element.

So assume that you must have that element.

You can hide the <input> and interact with the datepicker via calls to its methods from your label events.

$(labelselector).click(function() {
    $(inputselector).datepicker('show');
});
like image 54
Ken Browning Avatar answered Sep 24 '22 18:09

Ken Browning


re: The positioning problem

The above suggestions didn't work for me to get a clean ui. I have my datepicker control activated when people click a label, and I didn't want the textbox shown at all (the above css attempt was close, but still the textbox got focus amongst other issues).

The solution for me was to make the textbox that the datepicker is attached to a < input type="hidden" .... /> This solved all the issues for me (I put the hidden input control just prior to the label so that the offset from the input control was right for my label).

Notably, setting a normal input controls style to display: none, or visibility: hidden, etc. did not work.

The reason this solution worked is due to the clauses in the source of the datepicker control that only perform certain functions if "type != "hidden".

like image 28
Joel Friedlaender Avatar answered Sep 22 '22 18:09

Joel Friedlaender