简体   繁体   中英

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

Just separate selector with comma:

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

In jQuery documentation it is called as Multiple Selector .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM