2

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){
        $('#events').empty();
        $('#events').append(
            '<option value="N/A">N/A</option>'
        );
        var newDate = new Date();
        var plusSevenDate = new Date(date);
        var minusSevenDate = new Date(date);        

        plusSevenDate.setDate(plusSevenDate.getDate()+7);
        minusSevenDate.setDate(minusSevenDate.getDate()-7);

        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>" ;
        $().SPServices({
            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");
                    $('#events').append(
                        '<option value="' + valueString + '">' + $(this).attr("ows_LinkTitle") + '</option>'
                    );      
                    $('#events').addClass("eventCSS");
                }); //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){
                    $('.eventField').show(); 
                    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.
                    queryEvents(date);
                    //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)
                    $('#ctl00_m_g_420b1f4d_1ab4_4265_99be_345a7b9015f2_ff141_ctl00_ctl00_TextField').val(eventName);
                    var eventID = eventName.split(';#')[0];
                    $('#ctl00_m_g_420b1f4d_1ab4_4265_99be_345a7b9015f2_ff381_ctl00_ctl00_TextField').val(eventID);
                }
                //if there's no value in the events, show the "no event" error message
                else{

                    $('.eventField').hide();
                    $('.noEvent').show();
                }
            }
            else{
            //if not event related, all default values returned to blank and events/no event messages are hidden
                $('.eventField').hide();
                $('#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
                $('#ctl00_m_g_420b1f4d_1ab4_4265_99be_345a7b9015f2_ff381_ctl00_ctl00_TextField').val('');
                $('.noEvent').hide();
            }
        });

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

0

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 Jun 22 '15 at 15:31
0

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 Jun 22 '15 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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