I have a custom dropdown choice field that is created using jQuery after a date is selected and a choice box is selected as "Yes." Here is that function:

    function queryEvents(date){
            '<option value="N/A">N/A</option>'
        var newDate = new Date();
        var plusSevenDate = new Date(date);
        var minusSevenDate = new Date(date);        


        plusSevenDays = plusSevenDate.toISOString().split("T")[0];
        minusSevenDays = minusSevenDate.toISOString().split("T")[0];

        CamlQuery = "<Query>" +
                    "<Where>" +
                        "<And>" +
                            "<Gt><FieldRef Name='StartDate' /><Value Type='DateTime'>" + minusSevenDays + "</Value></Gt>" +
                            "<Lt><FieldRef Name='StartDate' /><Value Type='DateTime'>" + plusSevenDays + "</Value></Lt>" +
                        "</And>" +
                    "</Where>" +
                "</Query>" ;
            operation: "GetListItems",
            webURL: "/admin/",
            async: false,
            listName: "Calendar",
            CAMLQuery: CamlQuery,
            CAMLViewFields: "<ViewFields><FieldRef Name='ID'/><FieldRef Name='LinkTitle'/><FieldRef Name='StartDate'/></ViewFields>",
            completefunc: function (xData, Status) {    
                $(xData.responseXML).SPFilterNode("z:row").each(function() {
                    var valueString = $(this).attr("ows_ID") + ';#' + $(this).attr("ows_LinkTitle");
                        '<option value="' + valueString + '">' + $(this).attr("ows_LinkTitle") + '</option>'
                }); //end xml function
            } //end completefunc
    }); //end spservices    

I need to be able to create an if statement that translates to "if this events option drop down contains more than just the default 'N/A' then show the field" but no matter what I've tried, it just keeps telling me that it contains only one option (when I know for a fact there are more than 10). Here's what I have right now for when that Yes or No field changes (onchange):

         $('#ctl00_m_g_420b1f4d_1ab4_4265_99be_345a7b9015f2_ff171_ctl00_DropDownChoice').on('change', function(){
            var choice = this.value; 
            var eventName = $('#events').val();
            var eventDD = $('#events');

            if(choice == 'Yes'){
                //when yes, the event choices will not appear unless there is a value to display    
                if(eventDD.find('option').length > 1){
                    var date = $('#ctl00_m_g_420b1f4d_1ab4_4265_99be_345a7b9015f2_ff31_ctl00_ctl00_DateTimeField_DateTimeFieldDate').val();
                    //function filters events based on "Start Date" provided and "Yes" for event-related. Seven days before and seven after.
                    //sets the value of sharepoint event field to the currently populated event when Yes is chosen
                    //this exists in case the event the user is looking for does not require them to search and select (first choice)
                    var eventID = eventName.split(';#')[0];
                //if there's no value in the events, show the "no event" error message

            //if not event related, all default values returned to blank and events/no event messages are hidden
                $('#events').val(''); //sets list value back to blank
                $('#ctl00_m_g_420b1f4d_1ab4_4265_99be_345a7b9015f2_ff141_ctl00_ctl00_TextField').val(''); //sets sharepoint event field to blank

Does anyone see something I did wrong here? Or is there another way I can do this?

2 Answers 2


Try selecting id using jquery selector

For example To get those that end with "ckbxEmp"

$(document).on("change", "[id$='ckbxEmp']", function () {
  • I don't think I quite understand what you're asking me to do, but all values are unique in the options.
    – Caola
    Commented Jun 22, 2015 at 15:31

Are you checking how many options are in #events before you add them (in queryEvents)?

  • I know for a fact there are 8 events listed.
    – Caola
    Commented Jun 22, 2015 at 15:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.