Avatar of Keisha Smith
Keisha Smith
 asked on

Modal Form Submit Button Not Working When Clicked

I created a modal dropdown form with four dropdown lists, with the fourth dropdown containing a link to a document to display once user clicks the submit button. All four dropdowns work fine, however when clicking the submit button the document does not display. The form simply continues to display, log indicates 'Cannot read property '0' of undefined'.. Any help is appreciated.

var selectedOptions = {};

        $('#link1').on('change', function () {
            var a = $(this).val();
            selectedOptions['1'] = a;
            selectedOptions['2'] = a;
            selectedOptions['3'] = a;
            if (a !== '') {
                for (var i = 0; i < dataSecondSelect[a].length; i++) {
                    $('#link2').append($("<option></option>")
                            .attr("value", dataSecondSelect[a][i])
                            .text(dataSecondSelect[a][i]));
                }
            }
        });

        $('#link2').on('change', function () {
            var a = $(this).val();
            selectedOptions['1'] = a;
            selectedOptions['2'] = a;
            selectedOptions['3'] = a;
            if (a !== '') {
                for (var i = 0; i < dataThirdSelect[a].length; i++) {
                    $('#link3').append($("<option></option>")
                            .attr("value", dataThirdSelect[a][i])
                            .text(dataThirdSelect[a][i]));
                }
            }
        });
        $('#link3').on('change', function () {
            var a = $(this).val();
            selectedOptions['1'] = a;
            selectedOptions['2'] = a;
            selectedOptions['3'] = a;
            if (a !== '') {
                for (var i = 0; i < dataFourthSelect[a].length; i++) {
                    $('#link4').append($("<option></option>")
                            .attr("value", dataFourthSelect[a][i].link)
                            .text(dataFourthSelect[a][i].form));
                }
            }
        });

    $('#clickButton').on('click', function () {
        var error = false;
        $(".error").remove();
        $(".validation-error").removeClass('validation-error');
        $('#myModal select').each(function () {
            // validate first
            if ($(this).val() === "") {
                var _message = "Please select an option";
                $(this).addClass('validation-error');
                $(this).after($('<div class="error"></div>').text(_message));
                error=true;
            }
        });

        if (error) { return; }
        // form is now validated so get the link
        var _index = $("#link4").val();
        var _form = dataFourthSelect[_index][0].link;
        resetForm($(this)[0]);
        $('#myModal').modal('hide');
        openDoc(_form);

    });

    function resetForm(e) {
        $(".error").remove();
        $(".validation-error").removeClass('validation-error');
        e.form.reset();
    }

</script>

Open in new window

HTMLJavaScriptjQuery

Avatar of undefined
Last Comment
Keisha Smith

8/22/2022 - Mon
Ron Malmstead

At what line number is the error throwing?

This error is very common actually, but you need to know what code line number is causing it first and foremost.
ASKER CERTIFIED SOLUTION
David S.

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Keisha Smith

ASKER
Removed the fourthdata select link.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes