0

I have a jquery, it repeats 5 times for 5 buttons. I'm not good in Jquery, so I want to ask how can I optimize my code in 1 function ? Thanks

$(function() {
    $("#frmEdit\\:idBtn1").datetimepicker({
        showOn: "both",
        buttonImage: "../../jquery/images/calendarIcon.png",
        buttonImageOnly: true,
        constrainInput: true,
        showButtonPanel: true,
        dateFormat: 'dd-M-yy    ',
        addSliderAccess: true,
        sliderAccessArgs: { touchonly: false }
    });
});
$(function() {
    $("#frmEdit\\:idBtn2").datetimepicker({
        showOn: "both",
        buttonImage: "../../jquery/images/calendarIcon.png",
        buttonImageOnly: true,
        constrainInput: true,
        showButtonPanel: true,
        dateFormat: 'dd-M-yy    ',
        addSliderAccess: true,
        sliderAccessArgs: { touchonly: false }
    });
});
..........

2 Answers 2

5

You can use multiple selectors. http://api.jquery.com/multiple-selector/

$("#frmEdit\\:idBtn1, #frmEdit\\:idBtn2")

If you have more than a few things to select, I would suggest setting a class, and use a class selector instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you all of you so much
0

You could simply create a function

function createDatePicker(id) {
    $("#frmEdit\\:" + id).datetimepicker({
        showOn: "both",
        buttonImage: "../../jquery/images/calendarIcon.png",
        buttonImageOnly: true,
        constrainInput: true,
        showButtonPanel: true,
        dateFormat: 'dd-M-yy    ',
        addSliderAccess: true,
        sliderAccessArgs: { touchonly: false }
    });
};

Usage

createDatePicker('idBtn1');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.