How do I apply a function to two elements?

I have two elements #DateTimeStart

, #DateTimeEnd

I would like to apply my datetimepicker

to both.

With this code I cant get 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 similar but with less code

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

      

+3


source to share


2 answers


Just a separate semicolon selector:

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

      



It's called Multiple Selector in the jQuery documentation .

+11


source


Assign both elements to the class: "datepickerclass" and then use the code below $('.datepickerclass').datetimepicker({ addSliderAccess: true, sliderAccessArgs: { touchonly: false } });


This way you can have any number of elements with a dumper attached



0


source







All Articles