3

I have two elements #DateTimeStart, #DateTimeEnd, I would like apply my datetimepicker to both.

With this code I cannot have the result.. any idea what I am doing wrong here?

$(document).ready(function () {
    // This does not work
    $('#DateTimeStart', '#DateTimeEnd').datetimepicker({
        addSliderAccess: true,
        sliderAccessArgs: { touchonly: false }
    });
});

What I am trying to achieve is like but with less code

// This code works
$(document).ready(function () {
    $('#DateTimeStart').datetimepicker({
        addSliderAccess: true,
        sliderAccessArgs: { touchonly: false }
    });  
    $('#DateTimeEnd').datetimepicker({
        addSliderAccess: true,
        sliderAccessArgs: { touchonly: false }
    });
});
Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
GibboK
  • 64,078
  • 128
  • 380
  • 620

2 Answers2

11

Just separate selector with comma:

$("#DateTimeStart, #DateTimeEnd").datetimepicker({
    addSliderAccess: true,
    sliderAccessArgs: { touchonly: false }
});

In jQuery documentation it is called as Multiple Selector.

VisioN
  • 132,029
  • 27
  • 254
  • 262
  • May I ask you an explanation? In my code I have the elements separated by comma but it does not work. Your version works great. It is not good use ''? – GibboK Feb 06 '13 at 08:46
  • Your example code has them separated by quotes. The correct code is a single string. – AlienWebguy Feb 06 '13 at 08:47
  • Ok you have reply to my comment with your edit. Many thanks I will accept your answer. – GibboK Feb 06 '13 at 08:47
  • Wow 7 upvotes in a minute for a simple jQuery selector. Good job lol :) – AlienWebguy Feb 06 '13 at 08:47
  • 1
    @GibboK `$("#element1, #element2")` is not the same as `$("#element1", "#element2")`. The second is the same as `$("#element2").find("#element1")`. – VisioN Feb 06 '13 at 08:48
  • @AlienWebguy Believe me, [some](http://stackoverflow.com/a/178450/1249581) can get [even](http://stackoverflow.com/a/306904/1249581) more [upvotes](http://stackoverflow.com/a/196687/1249581) ;) – VisioN Feb 06 '13 at 08:51
  • Haha yea 2008 questions are flooded with votes. +1 by the way ;) – AlienWebguy Feb 06 '13 at 09:00
  • @AlienWebguy Yeah, and *the very most* of popular questions already have answers. So to get really high graded answer nowadays is close to impossible. Hope this answer will also have +1000 some day in future :D – VisioN Feb 06 '13 at 09:04
0

Assign a class to both the elements say 'datepickerclass' and then use the below code $('.datepickerclass').datetimepicker({ addSliderAccess: true, sliderAccessArgs: { touchonly: false } });
This way you can have any number of elements having datepicker attached

asifsid88
  • 4,341
  • 18
  • 29