Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attach to multiple inputs? #2

Closed
mgibbs189 opened this issue Oct 10, 2012 · 20 comments
Closed

Attach to multiple inputs? #2

mgibbs189 opened this issue Oct 10, 2012 · 20 comments

Comments

@mgibbs189
Copy link

How do I attach Pikaday to multiple inputs?

E.g. a typical jQuery approach would look something like this:

$('.date-picker').pikaday();
@mgibbs189
Copy link
Author

From what I can tell so far, the easiest way would involve having to create a loop:

var picker = [];
$('.date-picker').each(function(idx) {
    picker[idx] = new Pikaday({ field: $(this) });
});

Is there a simpler way?

@dbushell
Copy link
Member

Yep that would be the quickest way. If you don't need to reference the instance later you can just do:

$('.date-picker').each(function() {
    new Pikaday({ field: $(this) });
});

Or better yet do this inside the loop:

$('.date-picker').each(function() {
    $(this).data('pikaday', new Pikaday({ field: $(this) }));
});

then later you can access:

// field is a single .data-picker jQuery reference
field.data('pikaday').nextMonth();

In future I may write a small (optional) jQuery wrapper so that you can use it like other plugins.

@dbushell
Copy link
Member

For anyone who has never used .data() in jQuery (API reference) it allows you to store key/value data against an element. It will also automatically pull in content from HTML5 data-* attributes when the element is first selected via jQuery.

@mgibbs189
Copy link
Author

Right. The .each() and .data() approach is great. A jQuery wrapper (bundled within pikaday.js) would go a long way towards people making the switch away from jQuery UI datepicker.

@dbushell
Copy link
Member

Not a bad idea :)

For now I've written a jQuery version (see /plugins in the repo) — it's basically just the normal version with a wrapper heading it. I'll consider throwing this into the pikaday.js for simplicity. That or rewrite the whole jQuery version to take advantage of things like class and event management. A jQuery-dependent version would be smaller.

@mgibbs189
Copy link
Author

Cool. Well it looks like you're on the right track. Looking forward to seeing how this all pans out.

graste pushed a commit to graste/Pikaday that referenced this issue Jan 5, 2015
Made to have (optional) input format(s) as well as an output format.
hachibeeDI pushed a commit to hachibeeDI/Pikaday that referenced this issue May 24, 2016
@ronilaukkarinen
Copy link

ronilaukkarinen commented Mar 7, 2017

I bashed my head with this for couple of hours and didn't get any of the examples to work... no errors in log, but no datepickers either.

Ended up just copying it like this:

// Datepickers
var startdate = new Pikaday({
  field: $('.reservation').find('#startdate')[0],
  format: 'DD/MM/YYYY',
  firstDay: 1
});

var enddate = new Pikaday({
  field: $('.reservation').find('#enddate')[0],
  format: 'DD/MM/YYYY',
  firstDay: 1
});

@hugomelo
Copy link

Passing $(this) (jQuery object) doesn't work for me:

var picker = [];
$('.date-picker').each(function(idx) {
    picker[idx] = new Pikaday({ field: $(this) });
});

It only work when I use only this. The direct element.

var picker = [];
$('.date-picker').each(function(idx) {
    picker[idx] = new Pikaday({ field: this });
});

@giovanigenerali
Copy link

giovanigenerali commented May 31, 2017

@hugomelo try to use like this $(this).get(0) or $(this)[0] http://api.jquery.com/get/

var picker = [];
$('.date-picker').each(function(idx) {
    picker[idx] = new Pikaday({ field: $(this).get(0) });
});
var picker = [];
$('.date-picker').each(function(idx) {
    picker[idx] = new Pikaday({ field: $(this)[0] });
});

@Dennniss
Copy link

Hello, i am using Ninja form, is there a way to select different dates in the Pickaday calendar in the same form field separeted with , for example?

@jtlindsey
Copy link

In some cases the .each and $(this) wasn't working for me. The alternative that worked was grabbing the element in the callback:

$('.datepicker').each(function(i, e) {
  new Pikaday({
    field: e,
    ...
  });
});

@chethan1095
Copy link

can we select only time using pikaday

@hugomelo
Copy link

@chethan1095, there's an explaination in the README

@gresbtim
Copy link

what's the no jquery equivalent?

@eLIANT-TechnologyServices

On Mar 7, 2017, ronilaukkarinen said this method worked for him, but I'm getting nothin. When I applied Pikaday to just one input, it worked fine. But trying to apply to more than one produces no pop-up calendars on any of the input fields.

For context, it's an on-line job application form.

var App = new Date();
App.setDate(App.getDate() - 1);
var pickerApp = new Pikaday({
  field: document.getElementById('Applied-before-date'),
  maxDate: App
});

var Emp = new Date();
Emp.setDate(Emp.getDate() - 1);
var pickerEmp = new Pikaday({
  field: document.getElementById('Employed-before-date'),
  maxDate: Emp
});

var bd = new Date();
bd.setDate(bd.getDate() - 5479);
var pickerBD = new Pikaday({
  field: document.getElementById('Birthdate'),
  maxDate: bd
  defaultDate: bd

@milholen
Copy link

milholen commented Aug 23, 2020

@eLIANT-TechnologyServices the working code examples above are compatible with jQuery syntax, your code examples are native JS. I don't think the non-jQuery version of Pikaday supports this. See issue #123

Either add jQuery to your project (if you have a ton of fields to initialize) or if your app has a limited number of fields you can just instantiate Pikaday for each known field.

@eLIANT-TechnologyServices
Copy link

@gresbtim
Copy link

lmao. Philip, since this is my company account i can't flame you.

but

$('input.datep').pikaday({ firstDay: 1 });

$('input.datep') is usage of the jQuery library and returns a collection of elements.

.pikaday( then calls the plugin "pikaday" for each of those elements and { firstDay: 1 } are the parameters for pikaday

have a nice day.

@eLIANT-TechnologyServices
Copy link

@gresbtim
Copy link

jquery is a library written in javascript
https://jquery.com/

you add the jquery source to your html site and then can use jquery functions within javascript code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests