GibboK GibboK - 7 months ago 13
Javascript Question

How to apply a function to two elements?

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 }
});
});

Answer

Just separate selector with comma:

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

In jQuery documentation it is called as Multiple Selector.

Comments